Develop apps faster using Data Binding – Part 1

1. Data Binding

Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and APIs which helps developers to create an application in an easy way and having followed proper architecture and able to achieve good performance. “Data binding” is one of them.

What is Data Binding

It is a mechanism to make a bridge between Presentation layer and Model layer, where Presentation layer contains UI widgets like TextView, EditText, List etc. and Model layer contains entities which hold information.

Why Data Binding?

When “Data binding” was not there, we are using traditional way to display model information in UI using “getter” and “setter” methods. Like, we have a “Model” class which represents entity information and we have references to UI widgets and getter methods from which we fetch data and display it over UI.

And, below is code to display data in Presentation layer.

In above example, there is unnecessary code. Those “setText” methods are boilerplate code and a bit time consuming. We already have “IDs” of each widget then why to write code to map “Model” to it? Right!!

So to overcome this problem, Data binding is here and it provides easy a way to reduce your work by mapping model object in your layout.

How to use Data Binding

To implement Data Binding in your application, you need to follow below steps.

Step 1

Create a Project and open main Gradle file of the project.

Main Gradle

Main Gradle

Add below two lines in your dependencies tag

This will add a dependency to your project.

Step 2

Now open your module’s Gradle file. We require to add a plug-in to our project.

Your whole Gradle file will look like this

Step 3

An environment is ready with all dependencies integration. Let’s take an example of a person having his name and age. We will create a Model class with this properties first.

You can see here, we have not created any “getter/setter” methods.

Step 4

Open your layout file and add layout tag as root of it. It is necessary to have layout tag and data tag. It holds information about which model(s) is/are using in layout.

Here, data has variable tag describes which properties will be used in current layout. name defines name space and type defines package path of Model.

We tell layout that I want to use User model class in this layout. Now tell each widget which properties they are going to use. To access any member of User class, below is syntax

Syntax
android:text="@{user.memberName}"

Example
android:text="@{user.displayName}"

Here is full layout file code.

If you noticed, I used android:text="@{String.valueOf(user.age)}" in layout. Why? Because age is an Integer argument and we can not directly set it to TextView.

Step 5

We are almost done with mapping. Now, we require to bind views to model in our Activity

When you use data tag in your layout, Android will auto generate a class with layout filename and a suffix “Binding”. For example, your layout file name is main_activity.xml then your autogenerated class name will be MainActivityBinding. This class holds all binding i.e. user in our case.

Open your activity class and write below code.

Full Activity code.

Step 6

You’re done! Run the application.

Data Binding Output

Data Binding Output

Download

Source code for this article is available here

You may also like to read

MaxHeightScrollView in Android using Android Studi... In Android, there are two constants for providing height and width of any view. But if you provide any specific height to any view, it won't be change...
APK Analysis : Spotlight on WhatsApp | How to use ... Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK after the build process is complete. In th...
Auto Restart application after Crash/Force Close i... In an Android application, we usually got the "Force Closed" error if we didn't get the exceptions right. Everyone has question about "How can I resta...
Create custom Toast in Android Studio In this article we learn how to create Custom Toast in Android Studio. Toast A Toast provides simple feedback in a small popup about an operatio...
Share on Facebook1Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone