Image for post
Image for post
Photo by Tabl-trai under Creative Commons licence

and making ViewPager2 usable

RecyclerView is one of the main building blocks of Android user interfaces today. It’s more capable and flexible than its ListView predecessor but this also leads to more complexity and new problems.

One core feature of RecyclerView is the externalization of its layout engine to components called LayoutManagers, which are no longer limited to vertical scrolling only. Most implementations allow to choose between horizontal and vertical scrolling and a LayoutManager could technically support both at the same time.

RecyclerView also supports nested scrolling by implementing the NestedScrollingChild3 interface, which means it can cooperate with a parent view implementing NestedScrollingParent3 when…


Life is too short to waste time on writing Parcelable code

Image for post
Image for post

Two years ago, I wrote about how you can leverage features of the Kotlin programming language to manually write your Android Parcelable implementations in the most concise and readable way. Does it mean that I always prefer writing this code manually rather than letting a library or tool generate it for me? Of course not: like most developers, I believe that the best code is the code you don’t have to write. But I expect the tools I use to meet my quality standards.

That’s why I tend to be conservative about the dependencies I add to my projects and…


A match made in parser heaven

Image for post
Image for post

Moshi is a modern JSON library for Android and Java from Square. It can be considered as the successor to GSON, with a simpler and leaner API and an architecture enabling better performance through the use of the Okio library. It’s also the most Kotlin-friendly library you can use to parse JSON files, as it comes with Kotlin-aware extensions.

In this article I’m going to demonstrate how to take advantage of features from both the Moshi library and the Kotlin language itself in order to write efficient and robust JSON parsers.

The example model and JSON file

Consider the following model representing a person:

class Person(val…


Image for post
Image for post

I’m the happy owner of a Lenovo Ideapad laptop (model 710S-13IKB). It’s comparable to the Dell XPS 13 and runs beautifully under Linux, but one detail has always been bugging me: Lenovo only provides BIOS updates for its Ideapad laptops in the form of Windows 10 executable files. System firmware updates are important, especially to mitigate newly found vulnerabilities like Meltdown and Spectre. Since I wiped Windows off my machine a long time ago, I was wondering if there was an alternative (and secure) way to flash firmware updates on it.

And it turns out there is! After months of…


LiveData and the Fragment lifecycle

The new Android Architecure Components are soon to be announced as stable after a few months of public testing.

A lot has already been written about the basics (starting with the very good documentation) so I won’t cover them here. Instead I would like to focus on important pitfalls that are mostly undocumented and rarely discussed and may cause issues in your applications if you miss them. In this first article, I’ll talk about our beloved Fragments.

Edit (14 may 2018): Google has finally fixed the issue in support library 28.0.0 and AndroidX 1.0.0. See solution 4 below.

Edit (13…


object has its limits

In Kotlin, the singleton pattern is used as a replacement for static members and fields that don’t exist in that programming language. A singleton is created by simply declaring an object.

object SomeSingleton

Contrary to a class, an object can’t have any constructor, but init blocks are allowed if some initialization code is needed.

object SomeSingleton {
init {
println("init complete")
}
}

The object will be instantiated and its init blocks will be executed lazily upon first access, in a thread-safe way. To achieve this, a Kotlin object actually relies on a Java static initialization block. …


Image for post
Image for post

Update (november 2019): I published a detailed new article about how to use Parcelize to have your Parcelable boilerplate code generated automatically by a Kotlin plugin. This is the preferred method to implement Parcelable in Kotlin today, even if there are still exceptional cases for which you want to write this code manually in Kotlin. The following article will help you for those cases.

Implementing the Parcelable interface and maintaining this code remains a painful day-to-day task for Android developers. …


and how Kotlin generic types are superior to Java’s

There is an Android API I hate using because of a specific confusing method call. I’m certain there are many other examples of similar methods in the Java world. What’s interesting about it is that it illustrates how the limitations of a programming language (Java in this case) can lead to forcing developers to write unnecessarily cryptic code just to call a single method.

It’s also a good opportunity to explore the obscure world of generic types and variance that even experienced developers often struggle to fully understand.

While I’ll be using the Android platform as an example, the problems…


Delegated properties and ranges

Following the overwhelming positive feedback I received after publishing the first two parts of this series about the Kotlin programming language, including a mention by Jake Wharton himself, I’m happy to continue the investigation. Don’t miss part 1 and part 2.

In this part 3, we’ll reveal more secrets of the Kotlin compiler and provide new tips to write more efficient code.

Image for post
Image for post

Delegated properties

A delegated property is a property whose getter and optional setter implementations are provided by an external object called the delegate. This allows reusable custom property implementations.

class Example {
var p: String by Delegate()
}

The delegate…


Local functions, null safety and varargs

This is part 2 of an ongoing series about the Kotlin programming language. Don’t forget to read part 1 if you haven’t already.

Let’s take a new look behind the curtain and discover the implementation details of more Kotlin features.

Image for post
Image for post

Local functions

There is a kind of function we did not cover in the first article: functions that are declared inside other functions, using the regular syntax. These are called local functions and they are able to access the scope of the outer function.

fun someMath(a: Int): Int { fun sumSquare(b: Int) = (a + b) * (a + b) return sumSquare(1)…

Christophe Beyls

Android developer from Belgium, blogging about advanced programming topics.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store