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