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

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message