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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.