Data binding is a hot buzz word among Android developer quite recently. And why not? Because one thing I know about Data binding is that it gonna make us an offer that we can’t refuse. It will definitely change the way we code. Let’s see what does actually mean it.

Data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them. -Wikipedia

What does it offer us?

  1. Make findViewById totally obsolete
  2. Encourage to separate UI logic and business logic
  3. Make easy to synchronize between data sources and UI elements
  4. Provides a way to bind event listener using lambda or method reference from xml
  5. Provides a way to bind custom setters method or rename setters method of view’s attributes (Didn’t get it, don’t worry, we will see later.)

Quite a good deal, right?

How?

Let’s see how can we integrate in our Android app.

  • In build.gradle file, we will add following gradle property which enabled data binding,
build.gradle
  • In layout file, generally, we have ViewGroup (E.g. RelativeLayout or LinearLayout) as top in View hierarchy. But here, we will make <layout> tag as most parent tag or root tag. After adding it, build system will process it for data binding:
activity_main.xml
  • After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated ActivityMainBinding). We can set setContentView using DataBindingUtil like:
MainActivity.java
  • That’s it. Just run the project and check output. Amazing, isn’t it?

Have you noticed something strange? Yes exactly, now we don’t have to write findViewByIds. No more findViewByIds. Yay…

Image Source: Google

Let’s see another feature. Bind a data with UI element. So, we can remove setText method of TextView in above code. For that, we have to create a POJO class for that,

We have to do small change in layout activity_main.xml file:

activity_main.xml

Step 1: Here, <data> element is a way to binding data with UI element.

Step 2: <variable> element is a object of POJO which we bind with UI.

Step 3: We have added “@{personVO.name}” in text property of TextView. Generally, Expressions within the layout are written in the attribute properties using the “@{}" syntax

And last, we have to make small change in MainActivity.java like:

That’s it. Just think about complex views, we have to write so many lines for only initialization. But now, we don’t have to worry about such things. Data binding will take care of it.

Thanks google, for creating data binding to bail us out. :) 

This is just the beginning, there are so many cool features provided by Data Binding. Folks stay tuned… I am coming with more awesomeness of Data Binding. Finding out more on Data binding by reading this article.

Don’t forget to follow me on Medium and Twitter. If you liked the article, click the heart below so more people can see it! Thanks for reading!

Android Engineer @Bangalore

The (retired) Pub(lication) for Android & Tech, focused on Development