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"/> ...

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.