Android Data Binding Step by Step — Part 1

Data binding has made developers’ lives a lot easier and It’s a new era in android app development.

Today’s Motivation

“To book your next dream, read before bed…”

-Nanette L. Avery

What is Data Binding?

Android data binding is part of an android jetpack. If you’re a developer with some experience of android app development, you’ll be familiar with findViewById(), the method of declaring views in an activity.

Every time to obtain views references from these layouts, we use “findViewById” in our Activity/Fragment — Right?

How findViewById Works?

-Believe me It will clear your Data Binding concept

“Every time we uses findViewById function to get references to a view, Android System has to go through the view hierarchy and find it a runtime”

In Larger Android Application there can be many layouts and hundred of views and Android system recreates current screen of app in around 15–16 times in 1 second again and again, So of course it will reduce the performance of android app

-> Got the main problem? Now What’ the Solution?

Hang On! We Have DATA BINDING, Yes! You heard it right.

Well In next few lines you’ll understand how can we improve the performance with Data Binding

Remember we won’t talk about Kotlin Android Extensions here cause there are some known issues while using them and Now It is depreciated So

Forget About it. I Repeat Just Forget about it 😾

How Data Binding Improve Performance Under The Hood?

When we are using Data Binding we create a binding object that creates a reference to each view of layout. Once a Binding Object has been created, all the components of the app can access the views and other data through the binding.

This way android system do not need to go through the view hierarchy again and again hence performance of app will be improved a lot

Booooooom! How’s it? Interesting right? Still Miles to go 😅

Steps to Implement Data Binding

  1. Add this your app build.gradle:
android {
...
buildFeatures {
dataBinding = true
}
}

2. Wrap your layouts with <layout> tag in XML and get the view references

Let’s say the name of the Layout we wrapped with layout tags is activity_main. Using that Android data binding library will create a binding object with the name of ActivityMainBinding.

Data Binding Library takes the name of the XML Layout, make first letter of each name Capital, removes underscore and Adds Binding to the end.

3. Let’s go to the MainActivity.kt

Confused? Don’t worry, I will explain line by line

First of all now there is no need to use Old School “setContentView” to inflate the View.

We will use this new way of inflating the layout:-

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

Now you can reference any view of xml with this binding object like binding.nameTv, binding.ageTv. Important point to note here is that all of the views are stored in camel case

DataBindingUtil: A Utility class provided by the data binding framework to create binding from the layout provided.

Yohooooooo ……… You have done Basics of Data Binding

Let’s go in Depth of Data Binding

Data Binding with Objects

Consider above given MainActivity.kt and this layout file for Main Activity

Now, What are these other tags??????

  • <data>: A container for variables used inside the layout file. In this data tag, we can import our required model class, which needs to be bound with views:
<data>
<variable
name="student"
type="com.medium.Student" />
</data>
  • <variable>: describes a property that may be used within this layout.

This is how my Student class looks like:

So we’re going to bind our Student Object with Binding

Previous Code (Old Code):-

What if we were not binding objects with our Model ? Suppose we are getting Student object either from Api or from Local Database. Then Our code that time would be

val student : Student = getStudent()
binding.name = student.name
binding.address = student.address

New Code:-

val student: Student = getStudent()
binding.student= student

It’s Done! Yes…..

This is how we set our Model class / or Bind our View with binding object

Now you must be curious about this '@{}' using with views. Actually this is how Binding is getting done with that particular view. We can call it as One Way Data Binding —

This is how we are doing the Data Binding:-

Set databinding to true in Build.gradle, Insert Layout tags in Xml file, Use Data and Variable Tags if you Want to Bind Objects, Use DataBindingUtil to inflate views and get Data Binding Object, Get Views Reference by that Data Binding Object. Pretty Simple

Let me be clear that there are two types of bindings — One Way Data Binding and Two Way Data Binding 😅 Don’t worry these two things will be cleared in Next Part in more Details, Stay Tuned!

So I think We’re pretty Much done with the Moderate level details and Implementation.

Now Just Run your app and see the Magic.

This is all from my side!!

I hope you learned something from this article. And, If you have any suggestion/feedback, please do leave a comment so that we can all learn together :)

Please feel free to reach out to me on LinkedIn and If you are preparing for Interview This is for You — All Android Interview Questions at One Place

ENJOY YOUR CODING!

Software Engineer Android Apps @Deloitte USI — Consultant| Java | Kotlin | Sometimes Youtuber | Blogger