Crunching RxAndroid — Part 6

In the previous part of this series, we took a look on how we could hook to the lifecycle of the Activity or Fragment that is hosting our reactive routine. Now, it’s time to see how we can manage events of the Android system, with the help of a great tool.


Imagine you can avoid all the verbose structure needed for attaching listeners to your UI: wouldn’t you do it just for the sake of code readability? Luckily, there’s a library for that, and it’s pretty awesome too. RxBinding comes with a plethora of submodules that will help you manage the events of all the components provided with the Android SDK, from the standard to the support one, alongside with those provided with the latest Design Library. And there’s a Kotlin version as well!

The idea behind the library is to provide an Observable whenever the event we are interested in is raised. For example, this is what we would do if we were interested in the onClick event of a View:

RxView.clicks(fab).subscribe(aVoid -> onFabClicked());

Being an Observable, we can apply every transformation to the stream of data provided by just using the factory methods provided by the class RxView.

In a very similar way, we can override the behaviour of our app when the navigation icon (the one at the top-left corner of an Activity that has a parent) is tapped on by invoking the needed method:

RxToolbar.navigationClicks(toolbar).subscribe(aVoid -> onToolbarNavigationClicked());

Further explorations

Let’s now focus on what we would do in order to respond to the tap event on a menu item onto the toolbar and let’s compare it with what RxBinding gives us:


Simple, isn’t it? Now, for the final example, we will attach a TextWatcher to an EditText in the very old way, so that we can be notified of text changes in the given View:

usualApproachEditText.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {

public void afterTextChanged(Editable s) {}

It is verbose indeed! Can we do something about it? Let’s give it a try!



RxBinding is a powerful tool that will help you make your code more readable and, even more important, will allow you to manipulate Observables whenever the relevant events are raised.

As usual, you can find the source related to this article on GitHub.

Thanks to my friends Eugenio and Sebastiano for proofreading this article.