how to use data binding in android

The data binding library is a support library, which allows you to bind UI components from layout to source code in declarative format

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
Binding in Recylerview or Fragment:
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}" />

Share this content:


More Posts

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message