Wiring Up Views

You are ready to wire up your button views. This is a two-step process:

  • get references to the inflated View objects

  • set listeners on those objects to respond to user actions

Getting references to views

Now that the buttons have resource IDs, you can access them in MainActivity. Type the following code into MainActivity.kt (Listing 1.4). (Do not use code completion; type it in yourself.) After you enter each line, it will report an error.

Listing 1.4  Accessing view objects by ID (MainActivity.kt)

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton: Button
    private lateinit var falseButton: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ...

Get Android Programming: The Big Nerd Ranch Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.