RxJava meets Android Data Binding

Including code examples and working project

Probably every Android developer is using or at least heard about RxJava. Currently, there are lots of libraries that use the reactive approach, let RxPreferences, RxLocation, RxPermissions, RxWear, RxBinding serve as an example. But let’s go back to the beginning.

Here at Tango we are using RxJava in every Android project because it accelerates application development and helps us in the fight with callback hell. We have been successfully using the reactive approach with our custom made MVP framework and currently in all our new projects we are using it with MVVM design pattern. Unfortunately, some Android developers know RxJava only from examples which show how to implement GitHub repositories browser using Retrofit. However, RxJava is doing great in situations when we are dealing with data or event streams which can be really useful in combination with Android Data Binding. Let’s focus on the code below.

There is the binding expression in line #4 which set enabled attribute to true|false depending on viewModel’s firstName and lastName values. It works very well but in my opinion this approach has few disadvantages:

  1. You can’t write unit tests for this binding expression.
  2. You can’t reuse this binding expression between different layout XML files easily.
  3. It’s long and not as readable as it can be in ordinary .java file

Now let’s move to the next example.

I’ve changed line #4. Now it consists extremely simple and readable binding expression. In ViewModel, I’ve created helloButtonEnabled ObservableField which is bound to view’s enabled property. The interesting part begins on the #10 line where I’m combining two RxJava’s observables which are created from firstName and lastName observable fields .When both firstName and lastName values are not null and empty helloButtonEnabled is set to true, otherwise, it is set to false and helloText value is cleared.

It is possible to write unit tests for logic which is responsible for calculating helloButtonEnabled value which is later used for setting enabled binding.

Simple unit tests for MainViewModel class

Benefits of this solution:

  1. Important logic is in ViewModel
  2. Possibility to create unit tests for that logic.
  3. XML layout file is more readable.
  4. No code duplication.
Demo application which demonstrates standard Android Data Binding expression (first button) and reactive approach (second button). You can find it on github: https://github.com/TangoAgency/android-data-binding-rxjava

How is it possible?

Using RxJava with Android Data Binding is super easy! It is possible to register OnPropertyChangedCallback using ObservableField’s addOnPropertyChangedCallback method. The callback is called by android.databinding.Observable when an observable property has changed. With that knowledge we can wrap it with RxJava and then use it with all its awesomeness!

Update: In the example above I’ve used Observable.create, you can find updated version with Observable.fromEmitter usage on GitHub.

Using RxJava with Android Data Binding may improve your code quality and readability but remember that for some scenarios it may be an overengineering — you shouldn’t take a sledgehammer to crack a nut.

You can find example project on GitHub.

Are you looking for a great mobile development team that gets things done? Ping us at contact@tango.agency or @tango_agency on Twitter.