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>(R.id.sample_text).apply {
    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 :

<TextView
    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="http://schemas.android.com/apk/res/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)
or
val binding: ActivityMainBinding = ActivityMainBinding.inflate(getLayoutInflater())

and remove default contentview
setContentView(R.layout.activity_main)

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

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

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 .. https://developer.android.com/topic/libraries/data-binding/expressions#common_features

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) {
    Picasso.get().load(url).error(error).into(view)
}


<ImageView app:imageUrl="@{venue.imageUrl}" app:error="@{@drawable/venueError}" />
https://developer.android.com/topic/libraries/data-binding/expressions#common_features
https://developer.android.com/topic/libraries/data-binding/expressions#common_features
https://developer.android.com/topic/libraries/data-binding/expressions#common_features