RxJava and RxBinding

Overview

One of the benefits of using RxJava is the ability to compose asynchronous events in a more straightforward way. Complex UI interactions in Android can also be greatly simplified too, especially when there are multiple UI events that can trigger. To describe the interaction pattern with standard Android code, you would usually need to use a combination of both listeners, Handlers and AsyncTask (see this ebook for an example). With RxJava and RxBinding, this logic can be greatly simplified and you can describe the interaction patterns between multiple UI components.

Buttonbutton=(Button)findViewById(R.id.button);SubscriptionbuttonSub=RxView.clicks(button).subscribe(newAction1<Void>(){@Overridepublicvoidcall(VoidaVoid){//handle on click here}});// make sure to unsubscribe the subscription.

Another example handling text change on an EditText. Vanila android:

EditTexteditText=(EditText)findViewById(R.id.editText);editText.addTextChangedListener(newTextWatcher(){@OverridepublicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){}@OverridepublicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){// do some work with new text}@OverridepublicvoidafterTextChanged(Editables){}});

Same thing written with RxBinding support:

EditTexteditText=(EditText)findViewById(R.id.editText);SubscriptioneditTextSub=RxTextView.textChanges(editText).subscribe(newAction1<CharSequence>(){@Overridepublicvoidcall(CharSequencevalue){// do some work with new text}});// make sure to unsubscribe the subscription.

Example

For example, consider what you would need to do to implement a SearchView that will trigger an API call to fetch results related to the search term, so long as the term is at least three characters long and there has been at least a 100 milliseconds delay since the user last modified the search term in the view. With RxJava, this pattern can be described as follows: