September 30, 2021

how to use data binding in android

By lj

Data binding

The data binding library is a support library, which allows you to bind UI components from layout to source code in declarative format. Binding components in layouts makes it easy to maintain and simpler for us.

Why data binding

findViewById<TextView>( {
    text = viewModel.userName

Above is normal way to access textview from xml and set username to it, Now see below code which reduce this boilerplate code :

    android:text="@{viewmodel.userName}" />

Enable data binding

We can enable data binding in app by just enabling it from module’s build.gradle file. Edit build.gradle file as below:

android {
    buildFeatures {
        dataBinding true

Edit activity’s xml file and warp all code instead of xml tag inside layout tag as below :

<layout xmlns:android="">

Access View in Code file

To access UI, we need to create binding variable like below :

val binding: ActivityMainBinding = DataBindingUtil.setContentView(
            this, R.layout.activity_main)
val binding: ActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater())

and remove default contentview

To use binding with Recylerview or Fragment do following :

val listItemBinding = ListItemBinding.inflate(layoutInflater, viewGroup, false)
// or
val listItemBinding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false)
<data> tag in data binding
This tag is for importing or specifying variables which is directly used in xml file. 
Below is example of data tag with variable

    <variable name="user" type="com.example.User"/>

If we want to use enum or List operations we can import it as below( inside data tag ):

<import type="java.util.List"/>

It has list of supported operation which we can perform with variable inside xml file, you can see all the available options here ..

Binding Adapter

A very useful thing in data binding. Binding adapters are useful for other types of customization. For example, a custom loader can be called from a worker thread to load an image. The binding adapters that you define override the default adapters provided by the Android framework when there is a conflict. Example :

@BindingAdapter("imageUrl", "error")
fun loadImage(view: ImageView, url: String, error: Drawable) {

<ImageView app:imageUrl="@{venue.imageUrl}" app:error="@{@drawable/venueError}" />