Android Data-Binding

Introduction

If you’re a client developer, you’re probably heard about data binding.
It’s one of the most core concepts used in many known frameworks (such as angular.js , C# WPF, Swift ReactiveKit...)

So what’s all the fuss about ?

In simple terms, data binding is the process that synchronizes (binds) elements and business logic, so when the logic (data) is changed, the elements that bound to this data changes ,and vica versa ,hence ,they’re bound to each other.

It may not sound important at first, but that’s a really important principle, because it help to separate between the UI and the business logic, which is a basic principle of designing good architecture for our apps. (and used in MVVM and more design patterns).

Data binding types

If you’ve worked with xml before, you probably noticed that there is a way to cast it into object, and work only with that object, this process called unmarsalling.
And after we finished with that object , we may want cast it back to XML in the process called marshalling.

It’s possible, due to data-binding mechanism that binds xml data elements to the relevant class member, so we can access the element via the data, and when we change the data, it changes the xml element binds to it.
That’s called XML Data Binding.

Another binding type is UI Data Binding, that binds data to UI objects (text, editable texts…) , so we can control the UI element via the data, and vica versa.

Today , its more frequent to use UI Binding, and it used in many modern client side frameworks.

There are 2 ways to do binding :

One-Way Binding

One-Way binding happens when you change the source property, and in automatically changes the target property, but not the other way around.
Typical use case, can be when you want to update read-only ui field, so you want it change when the data changes, but there is no need change the data back, because the field won’t change, as it read only mode.

If you’re client developer, you probably used text change listeners, where some data is triggered on text change. It’s also sort of one-way binding.

Two-Way Binding

Two-way binding is full synchronization between source and target properties. Means that every change in the source property will reflect in the target, and vica versa.
Typical use can be in editable form/field.
E.g, we have a name text, which is editable, so we may want to sync it with the name data it represents, so every change we’ll do in the field it will reflect in the ui, and the other way around.

Eveet data-binding framework need to use binder , which is the part that creating the binding between the elements.

Android Data Binding

First introduced in Google I/O 2015 , Android have its own data binding library, which contains many features to simplify our work with data and write less boilerplate code.

Declarative Layouts

Typical way to implement data-binding in code is to declare the data where we would declare the views.
In android, the following can be a typical layout :

We need to annotate the getter of the bindable object in the @Bindable annotation.
Next, we need to call notifyPropertyChanged in the Setter of the object, with the generated name we declared in the @Bindable getter.

notifyPropertyChanged doing all the magic - it notifies all the relevant views attached, that the data has been changed, so it will change the view accordingly.

In order to to this, we needed to extend our model, but what if we already have other parent ?

In this case, we can use ObservableField.

Define binding via ObservableField

ObservableField is easier and flexible way to implement binding.
All there is need to configure, is the fields:

private static class User {
public final ObservableField<String> name = new ObservableField<>();
}

And that’s it!

Now, the use of BaseObservable / ObservableField with the binding set method, can provide us binding.

But, the binding if we change the data but what if we change the view ?

Two-Way binding

Until recently, there wasn’t simple way to implement two way binding, so the developers needed to manually write that part. (e.g use TextWatcher).
But now there is very simple way to do it, actually no more that 1 char of difference:

In the layout, instead of this :

@{user.name}

We use this :

@={user.name}

If we add ‘=’ it will support two way binding. Simple as that !

More cool features of the android data binding library worth to know

When we get the binding class using DataBindingUtil, it automatically generate reference to the view ids, and we can access it using camel case view id from that class.Which means that we now have global reference to every view id, and we no longer need to use findViewById (or ButterKnife)

It’s possible to use event binding in addition to data. (e.g, listener binding)
It’s possible to use data binding with RecyclerView using setVariable and executePendingBindings:

One really cool feature is @BindingAdapter, via this we can actually binding whole function to a view via custom attribute. A typical use case can be load image from web , and then bind that to ImageView, so we can create attribute imageUrl and then use in the view simple as :
app:imageUrl="@{imageUrl}"

Conclusion

Data binding is must-to-know concept that save a lot of boilerplate code, help to separate logic from the ui and simplify work with data in general.
Android data binding library make good use of it and adding more features to make our life easier.