Updated 15 December 2016
With the relaese of android M , the data binding came into android. As its a support library it supports back to API level 7. The data binding is a technique to bind the application logic to the layouts which was earlier a redundant work. You have to initialize the view using findViewById() and then you have to set the value for the view.
But with data binding you can bind your data directly to the xml without initializing them to variables. So here we will make a Sample project to understand data binding.
Firstly we have to support the binding, to do so add these lines of code to your project level gradle
1 2 3 4 |
dependencies { classpath 'com.android.tools.build:gradle:1.3.1' classpath 'com.android.databinding:dataBinder:1.+' } |
now add plugin to your app level gradle
1 |
apply plugin: 'com.android.databinding' |
Now your project is ready to support databinding. So lets start binding the data to our layout.
Suppose we want a layout to show a person data having name, age , phone number, email etc. For this we are having a Person class as
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 |
public class Person { String name,email; int age; long contact; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public long getContact() { return contact; } public void setContact(long contact) { this.contact = contact; } public Person(String name, String email, int age, long contact) { this.name = name; this.email = email; this.age = age; this.contact = contact; } } |
So the binded layout activity_main.xml will be like
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 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="person1" type="<YOUR PACKAGE>.Person"/> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person1.name}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person1.age}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person1.email}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{person1.contact}"/> </LinearLayout> </layout> |
With this xml file, the system will generate a Binding class with converting the xml name in Pascal case and suffixing Binding in it. As our xml name is activity_main so the binding class wil be named ActivityMainBinding.
Lets inflate the xml and get our data as required
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class MainActivity extends Activity{ ActivityMainBinding bind; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); bind = DataBindingUtil.setContentView(this, R.layout.bind_activity); callConnection(); } public void callConnection(){ //here you can get the person details we are adding details by out self //now we will bind this object with our layout bind.setPerson1(person); } } |
Initially it will show an error(red mark) but once project build is done it will work as expected. Basically data binding reduces the redundant and non logical code. You can use databinding with listview and recycler view as well and the places where you are inflating a single xml again and again. But we will discuss it later.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.