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.
- android {
- ...
- dataBinding{
- enabled=true
- }
- }
- 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;
- }
- }
- <?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>
Step 4: Establish DataBinding with the use of Java code.
- 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);
- }
- }

Join the conversation! Your thoughts help the community grow.