Data Binding Callbacks

One last step remains to get your buttons working: You need to hook onButtonClicked() up to your buttons.

Remember how you used data binding to put data into your UI? You can also use it to hook up click listeners and the like by using lambda expressions. (To refresh your memory on lambda expressions, see the section called Lambda expressions in Chapter 19.)

Add a data binding callback expression to hook your buttons up to SoundViewModel.onButtonClicked().

Listing 20.17  Hooking up your buttons (list_item_sound.xml)

<Button
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:onClick="@{() -> viewModel.onButtonClicked()}"
    android:text="@{viewModel.title}"
    tools:text="Sound name"/>

The next time you run BeatBox, your buttons should play sounds for you. However, if you try to run BeatBox with the green run button now, your tests will run again. This is because running the tests changed your run configuration – the setting that determines what Android Studio will do when you click the run button.

To run your BeatBox app, click the run configuration selector next to the run button and switch back over to the app run configuration (Figure 20.7).

Figure 20.7  Changing your run configuration

Changing your run configuration

Now run BeatBox and click on your buttons. You should hear your app make aggressive yelling sounds. Do not be afraid – this is what BeatBox was meant to do.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
44.197.238.222