Android Data Binding Library — I
Basics of Data Binding Library
The Data Binding Library is used to reduce the boilerplate code to bind application logic and layouts. Actually, this library reduces the effort which we give on writing the findViewById all the time to bind views in the Java code.
It’s a support library. So, it can be used from Android 2.1(API level 7+). To use this library Gradle 1.5.0-alpha 1 or higher and Android Studio 1.3 are minimum requirements.
I have also given an example of how to use this Data Binding Library in GitHub. To see the example go through the below link.
https://github.com/AvijitKarmakar/DataBindingAndroid
Changes in build.gradle:
Firstly, download the Support repository in the Android SDK Manager. Then add dataBinding element to the build.gradle file of the app module.
android {
....
dataBinding {
enabled = true;
}
}Data Model:
Creating a data model for UserName.
public class UserName {
private String firstName;
private String middleName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}Binding Data Model in Layout Files:
Data binding layout files start with a layout tag. This layout tag is used as the view or layout’s root tag. This layout tag is followed by data element and this data element is followed by variable element. This variable element defines which type of data will be used within this layout.
variable element’s name attribute defines the variable name and type attribute defines the variable type.
We can fetch those data from the layout file by these expression formats:
"@{variableName:modelAttribute}"
OR
"@{variableName:modelAttribute}"variableName : variable name defined in the layout file.
modelAttribute : attribute of the model of type variableName.
Now, let’s see an example of a layout file(simple_data_binding_activity.xml).
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/re/android">
<data>
<variable
name="userName"
type="ak.com.apps.android.databinding.model.UserName" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{userName.firstName}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{userName.middleName}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{userName.lastName}" />
</LinearLayout>
</layout>
Inflate Layout in Activity:
A Binding class will be created automatically based on the name of the layout file in Pascal case and appended “Binding” to it. For example: We have the layout file named simple_data_binding_activity.xml. So the name of the binding class will be SimpleDataBindingActivityBinding which we will use in the activity named SimpleDataBindingActivity.java.
public class SimpleDataBindingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflating layout
SimpleDataBindingActivityBinding binding =
DataBindingUtil.setContentView(
this, R.layout.simple_data_binding_activity);// Setting data for binding in layout
binding.setUserName(setUserNameData());
}
private UserName setUserNameData() {
UserName userName = new UserName();
userName.setFirstName("Avijit");
userName.setMiddleName("Nobody");
userName.setLastName("Karmakar");
return userName;
}
}
In the GitHub project, I have created a Simple Data Binding button in Main Activity. By clicking on that button we can see the described feature of Data Binding. Later I will show you some more features of this Android Data Binding Library.