This blog post will cover how you can use Data Binding library to quickly glue your application logic within your layout files. This will seriously reduce a lot of boilerplate code.

Contents

Setup

Layout Files

Binding Data

Event Handling

Layout file details

Data Objects

Setup

Data Binding is supported for Android 2.1 (API Level 7+) and Android Plugin for Gradle 1.5.0-alpha1 or higher is required. I’m using Android Studio 2.3 Beta 1

Add dataBinding Element to your app level build.gradle

android {

....

dataBinding {

enabled = true

}

}

Layout Files

Data-binding layout files begin with <layout> root tag followed by a <data> element and a <view> root element.

I’m creating a simple layout with three textViews that displays first name, last name and age of a User.

We need a data Object (Java class) to bind the data to the layout.

Binding Data

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was activity_main.xml so the generate class was ActivityMainBinding. This class holds all the bindings from the layout properties (e.g. the user variable) to the layout's Views and knows how to assign values for the binding expressions.The easiest means for creating the bindings is to do it while inflating:

Notice that I’m creating binding variable and a User variable and and setting the firstName, lastName and Age of the User and setting the user variable to the binding object.

After running the App you should have the data set to TextViews

Event Handling

Data Binding allows you to write expressions handling events that are dispatched from the views (e.g. onClick).

There are two ways to handle an event.

Method References: In your expressions, you can reference methods that conform to the signature of the listener method. When an expression evaluates to a method reference, Data Binding wraps the method reference and owner object in a listener, and sets that listener on the target view. If the expression evaluates to null, Data Binding does not create a listener and sets a null listener instead.

Listener Bindings: These are lambda expressions that are evaluated when the event happens. Data Binding always creates a listener, which it sets on the view. When the event is dispatched, the listener evaluates the lambda expression.

Note: The difference between Method Reference and Listener Binding is that in Method Reference the parameters of the method must match the parameters of the event listener(eg onClick(View) which takes a view as a parameter should have the same signature as that defined in our Java code). However in Listener interface a callback only your return value must match the expected return value of the listener

This is how our presenter class looks after adding those methods

Now let’s add a Presenter variable to our activity_main layout file

This is how the complete layout file looks

In MainActivity.java set the presenter to the binding

Now we have click listeners for the two buttons

Layout file Details

Imports

Zero or more imports elements may be used inside the data element. These allow easy reference to classes inside your layout file, just like in Java.

Let’s add a way to set visibility of firstName based on the boolean field in User class

We import the android View class within the <data> tag

Modify the TextView as follows

And in MainActivity java class set the binding as follows to add a fourth parameter which sets the visiblity

This will set the visibility of firstName to INVISIBLE

Data Objects

Any plain old Java object (POJO) may be used for data binding, but modifying a POJO will not cause the UI to update. The real power of data binding can be used by giving your data objects the ability to notify when data changes. To notify the data changes for a field implement the Observable class or extend the POJO class with BaseObservable.

This is done by assigning a @Bindable annotation to the getter and notifying in the setter.

We make slight change to onClickReference as follows

This will give a toggle effect to the visibility property of firstName