Complete Guide to Android Data Binding Library – Part -2

Before starting with Complete Guide to Android Data Binding Library – Part -2, Please visit Complete Guide to Android Data Binding Library – Part -1 so that it will be helpful to understand what is the Advanced level of Data Binding. In this post, we will understand bit more things about Data Binding. What is an Observable field? How can we change view value automatically using an Observable field? What is Binding Adaptor & How we can Bind variable directly to the view?

I hope you have gone through the Part -1, Now coming to the point we will discuss more Data Binding.

Working with Observable Data Objects:

An observable object can notify others about its data change & Data binding library allows us to use Observable fields or objects. Some observable fields are mentioned below for the example.

  • ObservableBoolean
  • ObservableByte
  • ObservableChar
  • ObservableShort
  • ObservableInt
  • ObservableLong
  • ObservableFloat
  • ObservableDouble
  • ObservableParcelable

To use these fields in over Binding variable or view model, we have to declare public final property as mentioned below.

To access the field value, use the set() and get() accessor methods, as follows:

How to use observable objects in data binding?

There are three ways to use observable fields or objects in data binding. Steps are mentioned below.

Extending baseobservable:

To use this method we need to extend BaseObservable class & all the getters would be annotated with @Bindable annotation. To notify the view of data change we need to use notifyPropertyChanged property to notify view about data change.

Implementing Observable:

We also can implement  Observable Interface & with declaring  PropertyChangeRegistry in the object to notify view about its data change.

Creating observable field:

This is the simplest way of using observables in Android data binding. we just need to create or declare observables fields. In this, as values being changes, the view gets automatically notified.

Binding Adaptors:

Binding adapters are responsible for making the appropriate framework calls to set values. One example is setting a property value like calling the setText() method. Another example is setting an event listener like calling the setOnClickListener() method.

The Data Binding Library allows you to specify the method called to set a value, provide your own binding logic, and specify the type of the returned object by using adapters.

You can also have adapters that receive multiple attributes, as shown in the following example:

You can use the adapter in your layout as shown in the following example. Note that @drawable/venueError refers to a resource in your app. Surrounding the resource with @{} makes it a valid binding expression.

For more information on Android Data Binding check it out official video on data binding.

Good Luck !!

 

I’m Dipendra, software developer, and designer who has written code in many of the language known to mankind.Passionate about Technology. In my free time, I love to write articles and build the code.

Leave a reply:

Your email address will not be published.

Site Footer