Wiring Up Widgets

You are ready to wire up your button widgets. 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 widgets

Now that the buttons have resource IDs, you can access them in MainActivity. Type the following code into MainActivity.kt (Listing 1.7). (Do not use code completion; type it in yourself.) After you save the file, it will report two errors. You will fix the errors in just a second.

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

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton: Button
    private lateinit var falseButton: Button override fun onCreate(savedInstanceState: ...

Get Android Programming: The Big Nerd Ranch Guide, 4th 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.