TheJadav

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>(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)
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

<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

Share this content:

Share:

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