Mallow's Blog

Android Architecture Component – Data Binding Library

I welcome everyone to the series of Android Architecture Components. Last time we have seen about  Room library, ViewModel and LiveData. Now we are going to see about one more interesting library called Data Binding Library which is also present in the Android Architecture Components. Data Binding Library is used to bind UI components in your layout to data sources in the app using a declarative format rather than programmatically.

Traditionally we developers are using findViewById() to bind layout views in XML with data programmatically. There are some popular data injection libraries like Butterknife which is used to bind data with a view. Following is the sample snippet of Butterknife:

@BindView(R.id.user) EditText username;

As developers, we are still facing difficulties in implementing proper data injection libraries due to some or more specific cases. Hence google themselves provide an optimum solution for proper data binding & reducing number of boilerplate codes compared to all. 

Implementation:

It is always good to have a sample demo application in order to understand the concept better.

1.  First, enable the data binding library in your project module (app module).

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

2. Wrap your XML code around layout tag and include the data tag with the respective variable tag to bind the UI components as below:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="user"
            type="com.example.databind.User" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<TextView
            android:id="@+id/tv_id "
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(user.id)}" />


        <TextView
            android:id="@+id/tv_user_name "
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

    android:layout_below="@id/tv_user_name "     
            android:text="@{user.username}"/>

    </RelativeLayout>
</layout>

3. Create a POJO class called User.java

import com.example.databind;

public class User{

private Integer id;

private String username;

public User(Integer id, String username){

this.id = id;

this.username = username;

}

// getter and setter method

}

4. Then in your respective activity class add the following data binding code

public class MainActivity extends AppCompatActvitiy{

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

ActivityMainBinding dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

User user = new User(1, “Arjun”);

dataBinding.setUser(user); // this will update the view

}

}

Conclusion:

Thus data binding library has proved to be a better, stable and easy to write (reduces the number of lines compared to other libraries).


Bringe Raj S B,
Android Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: