October 2019
Intermediate to advanced
434 pages
11h 54m
English
When writing Android View code with Kotlin, we can set click listeners on our Views using a lambda rather than an anonymous inner class, shown as follows:
button.setOnClickListener { // handle the event}
The lack of parentheses here makes this easier to read and to write. Additionally, we could combine this click listener lambda with a functional property for responding to the click:
var clickHandler: (() -> Unit)? = null...button.setOnClickListener { // handle the event clickHandler?.invoke()}
By exposing a public function property for responding to the click event, we can easily defer to that property within Button click listener and invoke the callback function only if it's non-null.
Read now
Unlock full access