Clean and safe view-binding with the Android Extensions plugin
In this recipe, we are going to explore the view-binding feature provided by the Kotlin Android Extensions plugin. It allows us to obtain references to View
type elements declared in the XML layout files in an easy and robust way, without using the original findViewById()
function. We are going to declare a TextView
element in the Activity
layout and obtain a reference to it in order to display a sample text in it.
Getting ready
In order to make use of the Kotlin Android Extensions plugin, we need to enable it in the Android project module-level build.gradle
script by adding the following declaration:
apply plugin: 'kotlin-android-extensions'
You can examine the implementation and configuration of recipes related to the Android framework in the AndroidSamples project available in this book's GitHub repository: https://github.com/PacktPublishing/Kotlin-Standard-Library-Cookbook/. To follow the Android-related recipes, you just need...