The findViewById() function is essential to creating view references in code. In this video, learn how to create view reference variables and specify their types.
- [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 time, …
Looking for study partners?Join the Associate Android Developer Exam study group
The Android app architecture constantly evolves. The introduction of the Kotlin programming language, new form factors such as Chromebooks, and reusable architecture components have made it faster and easier than ever to build high-quality Android apps. It is imperative that developers understand what the latest tools and recommendations are and how to use them to build out their applications. In this course, instructor David Gassner explains how to build an Android app that uses Kotlin instead of Java, add navigation functionality, and leverage the most useful architecture components such as LifecycleObserver, ViewModel, and LiveData, to create apps that are robust and easy to maintain over time.
- Handling lifecycle events
- Handling view events
- Updating display from new data
- Adding ViewModels to activities
- Subscribing to changes in LiveData values
- Adding a new launcher activity
- Starting activities with explicit intents
- Sharing data with implicit intents
- Adding a navigation drawer
- Adding a header to a navigation drawer
- Handling navigating drawer menu events