Android DataBinding in RecyclerView

Md Tarikul Islam
3 min readNov 13, 2018

--

Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. This reduces a lot of boilerplate code in your business logic that you usually write to sync the UI when new data is available.

DataBinding support library can also improve your app’s performance and help prevent memory leaks and null pointer exceptions. Google suggests using DataBinding for creating a relationship with your java code and XML layout file.DataBinding helps to remove findViewById() Java code.

I am not good at blogging, so sorry for my mistake.

Github

In this tutorial, I will try to build a Recyclerview using DataBinding. For enabling DataBinding in your android project add this line in your app/build.gradle file. In dependency, I also use two libraries. One is RecclerView another is Glide for loading and cashing Image from online.

android {
dataBinding{
enabled true
}
}

dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.android.support:recyclerview-v7:28.0.0'
}

Firstly, we have to reorganized our xml file. For Binding our xml file we have to write our all code in to <layout></layout> tag. layout tag is the parent i our every xml file. <data></data> tag is used for storing our value. <data> tag can create a connection with Java code. Within <data> tag we can also declare a variable.

In this XML file, I tried to get data from Java Object, string file and drawable file.

Mentioned: DataBinding still not support to get an image file from mipmap so we have store all image in the drawable folder.

Here is the common structure of data binding layout: Github

  1. Within <data> tag we can declared a variable using <variable> tag. This variable tag takes two attributes `name` type `type`.
  2. To bind a value, @{} annotation should be used. In the below layout, Flight name and from are bound to TextView using @{aFlight.name} and @{aFlight.from}. Next Textview I also bind flight time in a different way. For formatting flight time I used @{@string/flight_from_time(aFlight.fromTime)}. In string file, I create a formate and pass flight time and this return a formatted time data. Github

Sometimes you need to clean the project and rebuild for properly integrating binding. Then you have to work on your java code to connect the Activity with the layout file and passing data.

To bind the data in UI, you need to inflate the binding layout first using the generated binding classes. Below, FlightListItemBinding inflates the layout first.

The binding class follows a naming convention that is related to your XML file name. In this project my XML file name was flight_list_item.xml.That is way after rebuilding the project I got FlightListItemBinding class. Finally, holder.flightItemBinding.setAFlight(flight) binds the Flight object to the layout. Github

The code in Github:

Final output: Github

--

--