Fast lane to Data Binding in Android

Milan Vadhel
Mindful Engineering
3 min readApr 27, 2021

As an android developer, we all know that we are always finding something that reduces our code and makes code more cleaner. Apart from that set your data to your UI component is very boilerplate code like setText(), setImageResource(), etc.

There are many ways to avoid these things and access directly your UI component in your java or kotlin file like ButterKnife, Kotlin extensions, etc.
But the kotlin extension is now fully deprecated. So what the next…

Android provides us native support to access your UI component and bind your UI components directly to the Data source and that is called ViewBinding and Data Binding.

So in this article, we will learn some basics about view binding and explore how to add data binding to your project.

1. ViewBinding:

ViewBinding is a feature in an android that allows you to access all of the UI components directly via generated Binding classes. Once you enable the view binding in a build.gradle file, it generates Binding classes which contain all UI components references.

If your layout file name is like game_score.xml then it will generate its class with the name like GameScoreBinding.

ViewBinding in an Activity:

ViewBinding in a Fragment:

NOTE: Here I have used the backing property and assign a binding variable to null in the onDestroyeView() method because Fragments outlive their views. Make sure you clean up any references to the binding class instance in the fragment’s onDestroyView() method.

Advantages of View Binding:

Null safety:

Because View Binding directly creates a reference to views, there is not any risk of a null pointer exception.

Type safety:

The Generated binding class has the same class types fields as present in the XML file, so there are no risks for class cast exceptions.

2. Databinding :

Databinding is a support library provided by android to bind your UI components directly to the data source like ViewModel.

What is the main difference between View Binding and Data Binding?

  • With ViewBinding, the layouts do not need a layout tag.
  • ViewBinding is simply to bind your UI components to your code. While Data Binding is used to Bind your UI components to your Data Source.
  • ViewBinding is faster than Data Binding because there is no usage of an annotation processor to generate Binding classes.
  • ViewBinding does not support two-way binding.

So let’s begin to add data binding in your project step by step…..

How to use Data Binding in your project?

Data binding is a support library so you can use it with devices running Android 4.0 (API level 14) or higher.

Step 1:

Enable data binding in your module-level build.gradle file.

Step 2:

Create your Activity, Fragment like below.

<layout> To Generate the Data binding class for this UI.

Now Bind your ViewModel with CharacterFragment.

Here the item_charcter.xml for the Recyclerview item.

<data> To Bind your data to this UI.

<variable> Add your name and type which you want to bind to this UI.

@{} The expression language allows you to write expressions that handle events dispatched by the views.

With Expression language, you can use the following operators and keywords to manage your UI in a declarative format directly in the XML file.

Mathematical + — / * %
String concatenation +
Logical && ||
Binary & | ^
Unary + — ! ~
Shift >> >>> <<
Comparison == > < >= <= (Note that < needs to be escaped as &lt;)
instanceof
Grouping ()
Literals — character, String, numeric, null
Cast
Method calls
Field access
Array access []
Ternary operator ?:

Here I have used the Ternary operator to set the vector drawable based on character status.

Step 3:

Now time to bind your data to an XML file.

Here one magic thing I have used to load the image using a binding adapter.

Binding Adapter :

Binding Adapters are used to have custom setters to some property of your views. The most common use case I can think of is setting an image to an ImageView, where loading of the image is mostly done off the UI thread.

Explore more about Binding Adapter.

The Full sample is here.

Happy Coding!

--

--