Understanding and practice of RxJava & RxBinding of Android part -2

In my last tutorial, I have explained the basic functions and features of RxJava2. I would be recommended to check part -1 post before continue reading this post because it will help you to better understand this post. Here is my last post of  Basic understanding and practice of RxJava2 function in Android part -1.

We will continue and learn few other awesome features and functions of RxJava2 that we can use in our android development programming. As we have seen from the part-1 the basic requirement will be same here also. What you need to do add the RxJava and RxAndroid dependency in your gradle file to get the Reactive feature. For using lambda feature you have to enable the jack compiler and java_1.8 compatibility. Here are the details.

In Rx world, one new Reactive feature incorporated that is called RxBinding. So the question is What is RxBinding?

To get the RxBinding feature, you need to add the RxBinding dependency in your gradle file.

RxBinding: RxBinding is a set of libraries that allow you to react to user interface events. Let’s take a look at examples. This is how Android developers typically react to a button click event: via the RxJava paradigm.

Now in a Reactive world, it has like that.

Let’s take a look at another example, this time with a text change listener for an EditText:

Now in a Reactive world, it has like that.

RxBinding will give the best Rx Architecture and makes your code structure quite good. I believe that RxBinding will provide the most efficient way of mapping listeners to Observables. It is working quite good with any design pattern.

OK. Let’s take one example which is uploaded RxBinding video to check that how RxBinding is working with RxJava? In this example, I want to search the string from the recyclerview list while entering the text on edit text. Once the user has entered the text on edit text field then in the old fashion we used text watcher that watching the user action. TexWatcher triggered action afterTextChnage to update the user interface.

In RxWorld it should be instant search. So let’s modify this feature by using the RxBinding and RxJava. We need to convert the event to the string by using map() operation and will perform the search operation on computation schedule.

But here the problem is that RxJava emitter is emitting the data very fast but the consumer is not consuming those data as fast as the emitter. For that case, we need to use time frame to handle the multiple events and take the last emitting event data and will drop other events. Now here we have mapped data which we need to switch to List of Observable Type. For that, we need to use another RxJava function called SwichMap().

Now I have Observable Data Stream Which I need to subscribe to the adapter to notify RecyclerView list update. Here we can use Android Scheduler to switched to the main thread to update the user interface.

One thing always remembered, Once your job is done or task is completed then please unsubscribe the subscription to avoid the memory leak. Memory leak is very bad for the application. Here you can get the detail to why we need to take care memory leak?

Here is the result for an instant search of RxJava and RxBinding. Here I am doing a query on minimum length 2.

Here is the complete source code of this example.

and XML is here.

Wrapping up: This is a great example of RxBinding and RxJava and you have some ideas of how to cut a lot of boilerplate code from your Android apps using RxJava to handle all of your application’s UI events. RxBinding simple to use provides a consistent API for consumption and makes your application much more composable and reactive.

Here is an example of form validation by using RxBinding and updated RxJava2 function called Flowable to handle the back pressure on UI. Please check this post Rx binding with Rx java and Rx android in Android.

In my next tutorial of this Rx series, we will learn new reactive feature and functions. In the Reactive world, we should be thinking about the reactive that removed all the boilerplate code. Here is the part-3 of the RxJava2 feature. If you wondering Kotlin for android then I would be recommended to check all this post of Kotlin Category.

Please do subscribe your email to get every newsletter from this blog and if you feel that this post helps you then do not forget to share and comment below.

Happy coding 🙂

Sunil Gupta

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

You may also like...

1 Response

  1. Omar Beshary says:

    This is really great , thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.