Published Apr 03, 2018Last updated Apr 24, 2018

RxAndroid is the JVM implementation of ReactiveX in android. Some basics to know about RxAndroid are :

  1. RxAndroid was developed by Netflix.
  2. RxAndroid is a very useful library for handling asynchronous tasks in android.
  3. There are mainly three concepts in RxAndroid :
    (i) Observables.
    (ii) Observers.
    (iii) Subscribers



Observables are the objects that emit data i.e. these are the source from which the data comes.
Observables are an instance of the Observable class.
Observables emit data only if there is atleast one observer that subscribes for the data.
The Observable class has many static methods called operators for creating observables and these operators are responsible for the behaviour of the observables.


Observer is an object that consumes data emitted by the observable.
An Observable can have any number of Observers an observable emits data if there it atleast one observer which has subscribed for the data.
Observers are the instance of the interface observer.The interface consists of mainly three methods :
1. onNext()
2. onCompleted()
3. onError()

How it Works?

Whenever the Observable emits a data it calls the onNext() method of the Observer object OR Each time when an observable emits data the onNext() method of the Observer is invoked.If the observable emits an error then it is calls the onError() method of the observer exactly once.After finishing the data emission the observable calls sthe onCompleted() method of the observer.

Flow Diagram

For an example :
Here is a demo application that demonstrates how this works :
The first step to start working with RxAndroid is to add dependencies to the gradle scripts for the library.

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'

We need to add the rxjava dependency also for effective working.
Next make the UI of the app.Here we are maeking a basic UI with a button and a textview. Button has an onClick method as subscribeNow() which we will be defining in the MainActivity.java.
Here is the code for the UI :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


Next, in MainActivity.java file declare an Observable that emits String value and similarly declare an Observer that accepts String value.Also we declare the textview here :

private TextView textView;
private io.reactivex.Observable<String> mObservable;
private Observer<String> mObserver;

Below is the code where the Observable object emits the data.

mObservable = io.reactivex.Observable.just("RxAndroid is Amazing !!!!");

Now, we initialise the observer object so that we have an observer that subscribes to the observable object so that it can accept the data.On initialising an obsercer the code looks as below :

mObserver = new Observer<String>() {
           public void onSubscribe(@NonNull Disposable d) {


           public void onNext(@NonNull String s) {

           public void onError(@NonNull Throwable e) {


           public void onComplete() {


Now we want that whenever we click on the button the textview displays the observable described text. So, we subscribe the observable to observer in the method subscribeNow() as follows :

 public void subscribeNow(View view) {


As soon as the observer subscribes to the observable we need to update the textview, therefore we go to the onNext() method where we get the receive data from the observable and we can use it as we want.Below is the code :

            public void onNext(@NonNull String s) {

Here is the link to this demo project :


Discover and read more posts from MOHIT KUMAR
get started