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:
- 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.
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.
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!
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.
Demo that presents how to use RxJava with Android Data Binding ObservableFieldsgithub.com
Are you looking for a great mobile development team that gets things done? Ping us at email@example.com or @tango_agency on Twitter.