We all know that the most boring part of developer’s work is describing UI behavior depending on data change logic.
For Android apps, this is normally achieved by inflating the Activity (or Fragment) layout, locating the target UI element using findViewById, and finally assigning the appropriate data value to the element.
At Google I/O 2015, the new data binding support library was demonstrated, which can help developers perform all the above steps seamlessly using layouts (and properly defined classes and variables) only.
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,
Data binding layout files must be configured slightly differently from default layout files.
All layout files that intend to use data binding techniques must have a layout root tag. For a basic MainActivity class, a simple activity_main.xml layout would be something like this:
Data Binding Activity
At the moment, we have a layout file that is data binding capable. However, to utilize its data binding ability, we have to load it in a different way.
With data binding, a Binding class is auto generated from your layout file. The class is named using your layout file name by default. The default name is generated by capitalizing the first letter of each word after an underscore, removing all underscores, and adding ‘Binding’ to the name. As such, activity_main.xml will result in a class called ActivityMainBinding.
That’s it. Just run the project and check output.
While the ability to work without findViewById is a bonus, and the faster code is also nice, the real power of data binding becomes apparent when you begin to bind objects.
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:
And last, we have to make small change in MainActivity.java like:
And that is all. Run the application at this point, and you’ll find that the name is set to Mhamdi Semah, and the occupation to Engineer.
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.
Any plain old Java object (POJO) can be used for data binding, but modifying such a Java object will not cause the UI to update. To enable this, the data objects must be able to notify about data changes. There are three different data change notification mechanisms, Observable objects, observable fields, and observable collections.
Android provides the
BaseObservable class which you can extend. The data class is responsible for notifying when the properties change. This is done by assigning a
@Bindable annotation to the getter and notifying in the setter.
This listener is invoked on every update and it updates the corresponding views. This ensures that updates in the model updates also the UI.
This is just the beginning, there are so many cool features provided by Data Binding, check out the data binding android developer article.