Description
A demo of how to implement Data Binding in Android app
Each Activity in the demo app shows a sample implementation of data binding.
DataBindingExample alternatives and similar packages
Based on the "Data binding" category.
Alternatively, view DataBindingExample alternatives based on common mentions on social networks and blogs.
-
AlphabetIndex Fast Scroll RecyclerView
A Powerful AlphabetIndex FastScroller Library for Android's RecyclerView! -
LiveStream-Kt (Android) 📱
DISCONTINUED. LiveStream is a simple class which makes communication easy among different modules of your application. -
Some basics about view binding and explore how to add data binding to your project
Sample of Data Binding with MVVM + Clean Architecture. -
Data Binding Library
Official Android Data Binding Library to write declarative layouts and minimize the glue code necessary to bind application logic and layouts.
SaaSHub - Software Alternatives and Reviews
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of DataBindingExample or a related project?
Popular Comparisons
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.
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: Setbinding.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: Setbinding.setModel(new LoginModel());
in the Activity class. Step 4: Setbinding.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 customBindingViewHolder
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: Setbinding.recyclerView.setAdapter(adapter);
to the Activity class That’s it. Example:DataBinding4Activity