Android Data Binding

Have you felt that you need “something” to bind the data directly to the layout without writing some extra code!? “findViewById” method is scaring you!? The solution for these issues it’s called “Android Data Binding”.

This library offers both flexibility and broad compatibility so you could use it starting with Android 2.1 (API level 7+) and it was presented at Google I/O 2015.

It can be used with gradle 1.5.0-alpha1 or higher.

android data binding (4).png

Step 1:  To configure it just add in the gradle:

android {
    ...
    dataBinding {
        enabled = true
    }
}

Step 2: Create a POJO class called Book.

public class Book  
{  
    private String author;  
    private String title;  
    public Book(String author, String title)  
    {  
        this.author = author;  
        this.title = title;  
    }  
    public String getAuthor()  
    {  
        return this.author;  
    }  
    public String getTitle()  
    {  
        return this.title;  
    }  
    public void setAuthor(String author)  
    {  
        this.author = author;  
    }  
    public void setTitle(String title)  
    {  
        this.title = title;  
    }  
}  

Step 3: Declare a variable of your POJO class in the layout

Regarding the layout there are some requirements:

  1. the root of the layout file will be this tag
  2. is always put in the layout and it will contain the variables used to bind the layout
  3. contains the full name, including package name,  and the type describing of the variable used. This is actually the object for the POJO class.
  4. @{} container used for describing the expression. The variable created will be used to populate the view by accessing its properties and methods.
<?xml version="1.0" encoding="utf-8"?>    
<layout xmlns:android="http://schemas.android.com/apk/res/android">    
    <data>    
        <variable    
            name="book"    
            type="com.magdamiu.Book" />    
    </data>    
    
    <RelativeLayout    
        android:layout_width="match_parent"    
        android:layout_height="match_parent"    
        android:orientation="vertical">    
    
        <LinearLayout    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:layout_centerInParent="true"    
            android:orientation="vertical">    
    
            <TextView    
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"    
                android:text="DataBinding"    
                android:textAppearance="?android:attr/textAppearanceLarge" />    
    
            <LinearLayout    
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"    
                android:layout_marginTop="30dp"    
                android:layout_gravity="center_horizontal"    
                android:orientation="horizontal">    
    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:text="Author : " />    
    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:text="@{book.author}"    
                    android:textStyle="bold" />    
            </LinearLayout>    
    
            <LinearLayout    
                android:layout_width="wrap_content"    
                android:layout_height="wrap_content"    
                android:layout_gravity="center_horizontal"    
                android:orientation="horizontal">    
    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:text="Title : " />    
    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:text="@{book.title}"    
                    android:textStyle="bold" />    
            </LinearLayout>    
        </LinearLayout>    
    </RelativeLayout>    
</layout>

Just like Java expressions, DataBinding has some available operators for the layout files:

  • mathematical operators;
  • string concatenation;
  • logical operators;
  • binary operators;
  • unary operators;
  • bit shifting;
  • comparison operators;
  • instanceof;
  • grouping;
  • literals: string, numerical, symbolic, null;
  • type casting;
  • method calls and access to fields;
  • access to array elements and List;
  • “?:” ternary operator.
android:text="@{book.isSaved ?? book.author}"
android:text="@{book.title != null ? book.title : book.author}"

Step 4: Connect the layout with the Java code

public class MainActivity extends AppCompatActivity  
{  
    Book book;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
        book = new Book("Author Name", "The Android book");  
        binding.setBook(book);  
    }  
}  

The class ActivityMainBinding is auto generated based on the layout file. The name of the class was obtained from the name of the xml layout file which was “activity_main

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s