The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. In databinding set and get value from field directly to xml.
For example, the code below calls findViewById()to find a TextView widget and bind it to the userName property of the viewModel variable:
TextView textView = findViewById(R.id.sample_text); textView.setText(viewModel.getUserName());
<TextView android:text="@{viewmodel.userName}" />
One-way data binding :
Using one-way data binding, you can set a value on an attribute and set a listener that reacts to a change in that attribute:
<CheckBox android:id="@+id/rememberMeCheckBox" android:checked="@{viewmodel.rememberMe}" android:onCheckedChanged="@{viewmodel.rememberMeChanged}" />
in one data binding use this @{ } notation
Two-way data binding :
The Data Binding Library supports two-way data binding. The notation used for this type of binding supports the ability to receive data changes to a property and listen to user updates to that property at the same time.
Two-way data binding provides a shortcut to this process:
<CheckBox android:id="@+id/rememberMeCheckBox" android:checked="@={viewmodel.rememberMe}" />
The @={} notation, which importantly includes the “=” sign,
receives data changes to the property and listen to user updates at the same time.