Data Binding Callbacks

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

Just like you were able to use data binding to put data into your UI, you can 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 20.)

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

Listing 21.15  Hooking up your button (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 right-clicking to run 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 21.8).

Figure 21.8  Changing your run configuration

Screenshot shows changing the run configuration. In the screenshot displays, Beatbox in app menu with app option selected.

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
18.188.252.23