Working with Data Binding Android

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 2 3 4 5 6 |
android { ... dataBinding{ enabled=true } } |
Step 2 : Create a POJO class called Person.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Person { private String firstName; private String lastName; public Person (String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public void setFirstName(String firstName) { this.firstName=firstName; } public void setLastName(String lastName) { this.lastName=lastName; } } |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="person" type="com.androidgig.databindingdemo.Person" /> </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="AndroidGig 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="First Name : " /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person.firstName}" 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="Last Name : " /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person.lastName}" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </RelativeLayout> </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 2 3 4 5 6 7 8 9 10 11 12 |
public class MainActivity extends AppCompatActivity { Person person; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main); person=new Person("Ravi","Rupareliya"); binding.setPerson(person); } } |
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. Here is output of your first DataBinding program.
Update value with clickListener
We have seen how to bind value without declaring TextView object and without using setText() at runtime. Now we will see how to update those values on any Button/View click.
Step 1 : change your POJO class by extending BaseObservable. Update getter methods by using @Bindable annotation. You need to notify property for changing its value for that use notifyPropertyChanged in setter methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class Person extends BaseObservable { private String firstName; private String lastName; public Person (String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Bindable public String getFirstName() { return this.firstName; } @Bindable public String getLastName() { return this.lastName; } public void setFirstName(String firstName) { this.firstName=firstName; notifyPropertyChanged(com.androidgig.databindingdemo.BR.firstName); } public void setLastName(String lastName) { this.lastName=lastName; notifyPropertyChanged(com.androidgig.databindingdemo.BR.lastName); } } |
Here BR is auto generated class like R file.
Step 2 : Define button in xml file
1 2 3 4 5 6 |
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Update value" android:onClick="updateValue"/> |
Step 3 : Update click event and call setter method to update TextView.
1 2 3 4 5 |
public void updateValue(View v) { person.setFirstName("Android"); person.setLastName("Gig"); } |
In next posts we will see lot more about DataBinding, so keep visiting AndroidGig.
Download source code
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019