Binding to Data

With data binding, you can declare data objects within your layout file:

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools">
        <data>
            <variable
                name="crime"
                type="com.bignerdranch.android.criminalintent.Crime"/>
        </data>
        ...
    </layout>

And then use values from those objects directly in your layout file by using the binding mustache operator, @{}:

    <CheckBox
        android:id="@+id/list_item_crime_solved_check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:checked="@{crime.isSolved()}"
        android:padding="4dp"/>

In an object diagram, that would look like Figure 19.7:

Figure 19.7  The ...

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.