https://github.com/anitaa1990/databindingexample
A demo of how to implement Data Binding in Android app
https://github.com/anitaa1990/databindingexample
android android-development data-binding databindingutil demo loginmodel loginpresenter pojo
Last synced: about 1 month ago
JSON representation
A demo of how to implement Data Binding in Android app
- Host: GitHub
- URL: https://github.com/anitaa1990/databindingexample
- Owner: anitaa1990
- License: apache-2.0
- Created: 2018-04-30T07:53:46.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-20T10:32:41.000Z (about 6 years ago)
- Last Synced: 2025-03-26T00:51:21.984Z (about 2 months ago)
- Topics: android, android-development, data-binding, databindingutil, demo, loginmodel, loginpresenter, pojo
- Language: Java
- Size: 214 KB
- Stars: 11
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
1.Replace findViewById with data binding
Step 1: In layout file, make `````` 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```
2.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 `````` to the layout file.
Now generated binding class is named MainActivityBinding
That’s it.
Example: ```DataBinding2Activity```
3.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```
4.Event Handling using data binding
Step 1: In layout file, make `````` 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```
5.Custom Data Binding Implementation
Step 1: In layout file, make `````` 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.
``` @BindingAdapter("toast")
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```
6.Implement RecyclerView using Data Binding
Step 1: In layout file, make `````` 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```