RxJava meets Android Data Binding

Including code examples and working project

Radosław Piekarz
Oct 3, 2016 · 3 min read

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.

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
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.

Tango Agency

Tango Agency

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store