Data Binding Library In Android


If you make a layout into a declarative layout with the data binding you can avoid the expensive call findViewById(). Suppose you want to create a calculator application, then you have to place a number of buttons in the layout file for representing the digits and the operator.
That means you have to make a number of calls to findViewById() from the class file. If you convert that layout into a declarative layout with data binding then you can avoid the number of calls to findViewById. And also the data binding makes your layout more flexible.
In this article, I am going to create a simple Android application that demonstrates how to add the data binding library to your Android application and how to convert a layout into the declarative layout with data binding.
First, create your Android application as usual and add the data binding library into the Android studio project. This is already included with the Android SDK and we just need to enable data binding. For this open the module level Gradle file and add the following lines to the file.
  1. dataBinding {  
  2.     enabled true  
  3. }  
And click on the Sync Now button to add necessary libraries into your project.
Now, create a bean class that is going to be used in the layout file.
  1. public class Student {  
  2.     private String firstName;  
  3.     private String lastName;  
  4.     public Student(String firstName, String lastName) {  
  5.         this.firstName = firstName;  
  6.         this.lastName = lastName;  
  7.     }  
  8.     public String getFirstName() {  
  9.         return firstName;  
  10.     }  
  11.     public void setFirstName(String firstName) {  
  12.         this.firstName = firstName;  
  13.     }  
  14.     public String getLastName() {  
  15.         return lastName;  
  16.     }  
  17.     public void setLastName(String lastName) {  
  18.         this.lastName = lastName;  
  19.     }  
  20. }  
Now, I am going to make the layout file a declarative layout by adding the layout tag. And now out layout's root element would be layout tag like this.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layout xmlns:android="" xmlns:app="" xmlns:tools="">  
Now, we can pass any object from the Java class to the layout file by using the data tag. Inside data tag, we can declare our variables. Normally variables would have two attributes, they are name and type. The name can be any name that you want to use in the layout and type would be the Java class. So, here I am declaring the student class.
  1. <data>  
  2.     <variable name="student" type="com.example.pranavjdev.databindingsample.Student" />  
  3. </data>  
And in the layout I have created two text views and want to show the first and last name respsctively to each text view. For that we can use the variable name which we declared inside the variable and we can access the first name and last name respectively. 
  1. android:text="@{student.firstName}"  
Now our entire layout will look like the following
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layout xmlns:android="" xmlns:app="" xmlns:tools="">  
  3.     <data>  
  4.         <variable name="student" type="com.example.pranavjdev.databindingsample.Student" />  
  5.     </data>  
  6.     <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">  
  7.         <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_gravity="center_horizontal" android:text="@{student.firstName}" />  
  8.         <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_gravity="center_horizontal" android:text="@{student.lastName}" />  
  9.     </LinearLayout>  
  10. </layout>  
Now go to the Java class and remove the setContentView() for it and initialize the binding variable for the mainactivity. Normally the name of the binding class would be layoutfinename+Binding. Here the layout name is activity_main and hence the binding class name would be like AcivityMainBinding(activity_main.xml). Create an object for the binding class and load the layout in onCreate by using the following codes.
  1. ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);  
Now, we have got an object named binding, with the help of this binding we can access all the components of the layout file and also we the compiler would create getter and setter for all of its varibale. So we can pass the Student object by calling binding.setStudent();
  1. Student student = new Student("pranav","JDev");  
  2. binding.setStudent(student);  
So, the on create will look like;
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
  5.     Student student = new Student("pranav""JDev");  
  6.     binding.setStudent(student);  
  7. }  
And the output will look like,
Happy coding. Cheers...