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


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=""


            type="com.example.databind.User" />


            android:id="@+id/tv_id "
            android:text="@{String.valueOf(}" />

            android:id="@+id/tv_user_name "

    android:layout_below="@id/tv_user_name "     


3. Create a POJO class called

import com.example.databind;

public class User{

private Integer id;

private String username;

public User(Integer id, String username){ = 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{


protected void onCreate(Bundle savedInstanceState){


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

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

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




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 Comment

Your email address will not be published. Required fields are marked *