DataBindingExample
A demo of how to implement Data Binding in Android app
Each Activity in the demo app shows a sample implementation of data binding.
-
Replace findViewById with data binding
Step 1: In layout file, make <layout>
tag as most parent tag or root tag. After adding it, build system will process it for data binding.
Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.
That’s it.
Example: DataBinding1Activity
-
How can we change binding class name?
By default, binding class is generated based on the name of the layout file and placed in a data binding package under the module package. E.g. the layout file activity_main.xml will generate ActivityMainBinding. If the module package is com.an.demo, then it will be placed in com.an.demo.databinding directory.
We can change that by adding this tag <data class="MainActivityBinding">
to the layout file.
Now generated binding class is named MainActivityBinding
That’s it.
Example: DataBinding2Activity
-
How to bind data in layout file?
Step 1: Create a POJO class i.e. User
Step 2: Add element to the layout file. element is a way to binding data with UI element.
Step 3: Add element to the layout file. is a object of POJO which we bind with UI.
Step 4: add @{user.name}
in text property of TextView. Generally, Expressions within the layout are written in the attribute properties using the @{}
syntax
Step 4: Set binding.setUser(user);
in the Activity class
That’s it.
Example: DataBinding2Activity
-
Event Handling using data binding
Step 1: In layout file, make <layout>
tag as most parent tag or root tag.
Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.
Step 3: Create a POJO class i.e. LoginModel and add it as a variable to the layout file.
Step 4: Create a presenter class i.e. LoginPresenter and add it as a variable to the layout file.
Step 5: android:onClick="@{presenter::onClickButton2}"
is an example of event binding with Method reference That’s it.
Example: MainActivity
-
Custom Data Binding Implementation
Step 1: In layout file, make <layout>
tag as most parent tag or root tag. Create a POJO class i.e. LoginModel and add it as a variable to the layout file. i.e. LoginModel. Create a presenter class and add it as a variable to the layout file. i.e. LoginPresenter
Step 2: Create a separate adapter class for Binding i.e. DataBindingAdapter. Functionality of the custom binding class would be to display a Toast message. Create the binding method inside the customer adapter class.i.e.
public static void showToast(View view, String toast) {
if(toast != null && !toast.isEmpty()) {
Toast.makeText(view.getContext(), toast, Toast.LENGTH_SHORT).show();
}
}
Add the custom attribute to your view in the layout file.i.e. app:toast="@{loginInfo.loginMessage}"
Step 3: Set binding.setModel(new LoginModel());
in the Activity class.
Step 4: Set binding.setPresenter(new LoginPresenter());
in the Activity class.
That’s it.
Example: DataBinding3Activity
-
Implement RecyclerView using Data Binding
Step 1: In layout file, make <layout>
tag as most parent tag or root tag.
Step 2: Create a custom BindingViewHolder
class for reusability purposes.
Step 3: Create your recyclerview adapter class and item layout class. Set data & variable tag to the layout item class.
Step 4: Set binding.recyclerView.setAdapter(adapter);
to the Activity class
That’s it.
Example: DataBinding4Activity