From the course: Android Development Essential Training: App Architecture with Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Get view references with findViewById()

Get view references with findViewById() - Android Tutorial

From the course: Android Development Essential Training: App Architecture with Kotlin

Start my 1-month free trial

Get view references with findViewById()

- [Instructor] I previously described how to use synthetic view bindings that are offered as part of the Kotlin Extensions for Android, but there are many times when you might want to explicitly set your view references using a classic Android function called FindViewByID. This function is implemented as part of AppCompatActivity, and it also is available in fragments. And it can be called any time after you've called setContentView to select your activity layout files. Here are a couple of ways to structure the use of FindViewByID. One approach is to define your view references as properties of the class, but define them using the key word lateinit. So for example, I might say private lateinit var, that means that the variable is mutable. It can be changed later on. And I'll set its name as headline, and I'll set its type as TextView. Now this means I've declared it but I haven't initialized it. And so then in the onCreate function, before I reference this variable for the first…

Contents