Working with Data Binding in Android

Description

In I/O 2015 Google announced a data binding library for Android. With data binding, you create an ongoing link between an element in the user interface and a value. Data binding is the process that establishes a connection between the application UI and business logic.

As a developer we always try to do tasks with lesser code, findViewById and setText would be the things which will increase line of codes. Data binding eliminates needs of these methods.

DataBinding is a support library so you can use it with the version higher than Android 2.1. Now we will see step by step instruction for how to use DataBinding in real time.

Step 1: Enable dataBinding in your module level gradle.

  1. android {   
  2.    ...   
  3.    dataBinding{   
  4.       enabled=true   
  5.    }   
  6. }   
Step 2: Create a POJO class called Person.
  1. public class Person  
  2. {  
  3.     private String firstName;  
  4.     private String lastName;  
  5.     public Person(String firstName, String lastName)  
  6.     {  
  7.         this.firstName = firstName;  
  8.         this.lastName = lastName;  
  9.     }  
  10.     public String getFirstName()  
  11.     {  
  12.         return this.firstName;  
  13.     }  
  14.     public String getLastName()  
  15.     {  
  16.         return this.lastName;  
  17.     }  
  18.     public void setFirstName(String firstName)  
  19.     {  
  20.         this.firstName = firstName;  
  21.     }  
  22.     public void setLastName(String lastName)  
  23.     {  
  24.         this.lastName = lastName;  
  25.     }  
  26. }  
Step 3: Update your layout for DataBinding. To enable data binding for your layout, simply wrap your existing elements in a layout element. Declare a variable of your POJO class.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android">    
  3.     <data>    
  4.         <variable    
  5.             name="person"    
  6.             type="com.androidgig.databindingdemo.Person" />    
  7.     </data>    
  8.     
  9.     <RelativeLayout    
  10.         android:layout_width="match_parent"    
  11.         android:layout_height="match_parent"    
  12.         android:orientation="vertical">    
  13.     
  14.         <LinearLayout    
  15.             android:layout_width="wrap_content"    
  16.             android:layout_height="wrap_content"    
  17.             android:layout_centerInParent="true"    
  18.             android:orientation="vertical">    
  19.     
  20.             <TextView    
  21.                 android:layout_width="wrap_content"    
  22.                 android:layout_height="wrap_content"    
  23.                 android:text="AndroidGig DataBinding"    
  24.                 android:textAppearance="?android:attr/textAppearanceLarge" />    
  25.     
  26.             <LinearLayout    
  27.                 android:layout_width="wrap_content"    
  28.                 android:layout_height="wrap_content"    
  29.                 android:layout_marginTop="30dp"    
  30.                 android:layout_gravity="center_horizontal"    
  31.                 android:orientation="horizontal">    
  32.     
  33.                 <TextView    
  34.                     android:layout_width="wrap_content"    
  35.                     android:layout_height="wrap_content"    
  36.                     android:text="First Name : " />    
  37.     
  38.                 <TextView    
  39.                     android:layout_width="wrap_content"    
  40.                     android:layout_height="wrap_content"    
  41.                     android:text="@{person.firstName}"    
  42.                     android:textStyle="bold" />    
  43.             </LinearLayout>    
  44.     
  45.             <LinearLayout    
  46.                 android:layout_width="wrap_content"    
  47.                 android:layout_height="wrap_content"    
  48.                 android:layout_gravity="center_horizontal"    
  49.                 android:orientation="horizontal">    
  50.     
  51.                 <TextView    
  52.                     android:layout_width="wrap_content"    
  53.                     android:layout_height="wrap_content"    
  54.                     android:text="Last Name : " />    
  55.     
  56.                 <TextView    
  57.                     android:layout_width="wrap_content"    
  58.                     android:layout_height="wrap_content"    
  59.                     android:text="@{person.lastName}"    
  60.                     android:textStyle="bold" />    
  61.             </LinearLayout>    
  62.         </LinearLayout>    
  63.     </RelativeLayout>    
  64. </layout>  
Here person will be variable name through which we can access its property and methods. In above code we have written android:text=”@{person.firstName}” which will bind firstName value to that TextView. Don’t forget to replace com.androidgig.databindingdemo.Person with you package name

Step 4: Establish DataBinding with the use of Java code.
  1. public class MainActivity extends AppCompatActivity  
  2. {  
  3.     Person person;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);  
  9.         person = new Person("Ravi""Rupareliya");  
  10.         binding.setPerson(person);  
  11.     }  
  12. }  
You have noticed in above code that we have mentioned ActivityMainBinding but we have not declared it anywhere or where is that class? Your answer is it’s auto generated class for DataBinding. Binding class will be generated based on your layout file name. For Example if your layout name is activity_login.xml, you Binding class name will be ActivityLoginBinding.
Ebook Download
View all
Learn
View all