For the More Curious: More About Data Binding

Complete coverage of data binding is outside the scope of this book. And yet, reader, we must nevertheless try.

Lambda expressions

You can write short callbacks directly in your layout file by using lambda expressions. These are simplified versions of Java’s lambda expressions:

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

Like Java 8 lambda expressions, these are turned into implementations for the interface you use them for. (In this case, View.OnClickListener.) Unlike Java 8 lambda expressions, these expressions must use this exact ...

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.