Android RxBinding With Examples

In the previous tutorials, we discussed RxJava and some of its operators. Today we will discuss the RxBinding library in our Android Application.

RxBinding

We know that RxJava is a reactive event-based programming paradigm.
RxBinding is a set of support libraries to make the implementation of user interaction with the UI elements in Android easier.

To use RxBinding in your application you must include:

Using this along with the RxJava dependency:

To use RxBinding with Appcompat and other submodules we simply need to import their respective rxbinding libraries:

  • implementation com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:2.0.0'
  • implementation 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.0.0'
  • implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.0.0'
  • implementation 'com.jakewharton.rxbinding2:rxbinding-design:2.0.0'
  • implementation 'com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:2.0.0'
  • compile 'com.jakewharton.rxbinding2:rxbinding-leanback-v17:2.0.0'

we can use RxBinding features in our application.

RxView.click()

Typically, to set click listener events on a Button, the following is the piece of code that we write:

Now with RxBinding we can do:

Inside RxView.clicks we pass the View instance which is to be clicked.

If you are new to RxJava2, Disposable is equivalent to Subscription.
For more information on changes in RxJava2 refer here.

On the Disposable instance we can unsubscribe from the event by calling d.dispose();.
The Disposable instance holds a reference to the view. So if this code is defined outside the context of the Activity, you must unsubscribe from the event to prevent memory leaks.

RxView.click() returns an Observable. So we can add RxJava operators to perform transformations and chain implementations on it.

EditText TextChanges

Typically to add text change listeners on an EditText we need to implement the TextWatcher methods:

Using RxBinding we can do:

We pass the EditText inside RxTextView.textChanges.

Now let’s see how RxJava operators and transformations give us leverage.

Using Map operator

Using map operator we can change the data that is being sent.
For example, in the EditText we can change the CharSequence to a String

Using debounce operator

Using a debounce operator we can delay the event action.

For example, on the Button click, we can set a debounce of 2 seconds. This will run the operation after 2 seconds:

A debounce operation doesn’t run on the UI thread. It runs on the computational thread.
Hence you must call the main thread in the observeOn method after that as done above.

A debounce operator is commonly used in EditText, especially in a SearchView to let the user stop typing for a few seconds before running the action/requests.

Using throttleFirst

Unlike debounce which delays the action, throttleFirst operator is used in preventing repeated actions within a certain time interval.
ThrottleFirst is useful when it comes to preventing double actions when a Button is repeatedly clicked.

In the above code, the Button won’t show a Toast again until after 2 seconds.

Merging Multiple Button click actions

We can merge RxView.click() Observables in the following way:

Multiple Click Listeners on a Button

Using the share operator on the RxView.click() observable, both the Disposables created below are run when the button is clicked.

Let’s merge the above concepts in our Android Application with a few more operators:

Project Structure

android-rxbinding-project-structure (1)

Code

activity_main.xml

MainActivity.java

In the EditText, we’ve set a filter operator which doesn’t set the input text onto the TextView until the length crosses a threshold.

To clear all the Disposables, instead of calling disposable() on each of them separately, we can do the following as well:

The output of the application in action is given below:

android rxbinding output

Notice how many times the FloatingActionButton was clicked. But the Toast was displayed just once.

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply

%d bloggers like this: