Data Binding
The Data Binding Library introduced as part of Jetpack library allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
Features:
•Reduces lot of boiler plate code as no more need of findviewbyid.
•Helps to keep the code very organised as data for View can be set in layout itself instead of activity
- It offers flexibility and compatibility and It is supported API level 14 or higher.
Configure your app to use the data binding:
Open the app/build.gradle, add below line of code inside the android tags in gradle and sync project
dataBinding {
enabled = true}
Layout and Binding Expression in Data Binding
Data binding layout files are slightly different and start with a root tag of layout followed by a data element and a view root element.
Configure your app to use the data binding
Code Snippet:
<?xml version=”1.0" encoding=”utf-8"?>
<layout xmlns:android=”http://schemas.android.com/apk/res/android">
<data>
<variable name=”user” type=”com.example.User”/>
</data>
<LinearLayout >
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@{user.firstName}”/>
</LinearLayout>
</layout>
Note:
The user variable within data describes a property that may be used within this layout.
Expressions within the layout are written in the attribute properties using the “@{}” syntax
Here, the TextView text is set to the firstName property of the user variable
data class User(val firstName: String, val lastName: String)
Binding Data:
A binding class is generated for each layout file.
The above layout filename is activity_main.xml .
The corresponding generated class is ActivityMainBinding.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
binding.user = User(“Test”, “User”)
}
Binding Data in Fragments and others:
If we are using data binding items inside a Fragment, ListView, or RecyclerView adapter , then the syntax for binding data is as below:
val listItemBinding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false)
Event Handling:
Data binding allows you to write expression handling events that are dispatched from the views .
Event Handling:
public class EventHandler {
Context mContext;
public EventHandler(Context mContext) {
this.mContext = mContext;
}
public void onButtonClick(String name) {
Toast.makeText(mContext, “Now you are following “ + name, Toast.LENGTH_SHORT).show();
}
}
In Layout:
<variable
name=”handler”
type=”com.wave.databindingexample.EventHandler” />
<Button
android:id=”@+id/btnFollow”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/linlay1"
android:background=”@color/colorPrimary”
android:onClick=”@{() -> handler.onButtonClick(user.name)}”
android:text=”Follow”
android:padding=”8dp”
android:textColor=”@color/white” />
In Activity:
binding.setHandler(new EventHandler(this));
Two way DataBinding:
Most part of Databinding means Object set the value for layout attribute.
If we want view to update the data object value and data object to update view ,then it is called 2 — Way Databinding.
The syntax is as below :
<EditText android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=“@={user.firstName}”/>