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:

You can’t write unit tests for this binding expression. You can’t reuse this binding expression between different layout XML files easily. 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:

Important logic is in ViewModel Possibility to create unit tests for that logic. XML layout file is more readable. No code duplication.