Social

Why you should use Android Data Binding

Why I started used data binding

After my first blog post about Fastest way for creating RecyclerView Adapter, a couple of you post a comment why I don’t use Data Binding instead of Butterknife. To be honest, I didn’t try to use it simply because I’m used to using Butterknife. I know now that I was terribly wrong, so for every developer like me, here is post to try to convince you to start using data binding.

How to start

First of all, you will need to use Gradle 1.5.0 or above. Now open your app-level gradle and add this line under android:

1

2

3

4

5

android{

dataBinding{

enabled=true

}

}

Sync gradle and you are ready to go. I think that best way to learn new this is by going with a real example, so I will build this mini two screen Android app just to show you advantages of android data binding.

Best way to learn

My opinion is that in order to learn new thing, you need to practice on a real example, something like: it’s better to develop one app then read 10 tutorials. For this example, I will create one extremely simple app. The first screen will be RecyclerView when you click on the list item, it will open DetailsActivity with more info. I know that this is a stupid app, but still, it’s way bette then 8 Chinese octopus recipes.

Result

At first, I din’t like idea haveing login inside layout file, but result is much cleaner Java code, without all that findViewById boilerplate code. I encourage you to try it if you are not already, and you can start with my example on Github. There you can also find no-data-bindig branch so you can compare code side by side.