RxAndroid Example – RxAndroid Tutorial #2

rxandroid tutorial logo

In this RxAndroid Example Tutorial, we will continue our previous topic on RxAndroid tutorial‘s Getting started where we discussed about the basics of RxJava 2 and RxAndroid and its uses.

Now, we are going to integrate the RxAndroid in our Project and write a simple Android Project which shows how to declare an Observable, Observer and subscribe to the Observable to receive the data.

rxandroid example tutorial logo

In this tutorial, we will be creating a simple android project where upon whenever we make a simple button click we would be making our observable emit some data and at the same time our observer subscribes and emits data which we can see via toast messages. So let’s get started.

This article is part of RxJava Intro series. You can checkout the entire series here:

Below image demonstrates what we would be targeting at the end of the RxAndroid Example Project:

RxAndroid Example Project final output demo gif

The complete code for this tutorial is given at the bottom of the article.

Methods which we will be using in this RxAndroid Example tutorial:

create()Creates a new Observable. This method is available from Observable class

onNext(T value) – Emits a value. This method is from Emitter interface.

onComplete() – called upon completion of Emission of data. This method is also from Emitter interface.

3 Simple Steps to demonstrate RxAndroid Example:

1. Creating an Observable

Lets begin by creating the Observable which will emit the data when user clicks a button. Here I am using Observable’s create() method.

    mObservable = Observable.create(new ObservableOnSubscribe<Integer>() {
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                for(int i=1; i<=5;i++){

As we know that now Android Studio 3 supports Java 8 features, let us use Lambda expressions which we discussed in our Previous Tutorial on Android Java 8 Features Integration.

Now the above code can be reduced to the following using Lambda Expressions:

mObservable = Observable.create(e -> {
            for(int i=1; i<=5;i++){

2. Creating an Observer

Let us create an Observer and overriding all the required methods such as onSubscribe, onNext, onError, onComplete.

  mObserver = new Observer<Integer>() {
            public void onSubscribe(Disposable d) {
                Toast.makeText(MainActivity.this, "onSubscribe called", Toast.LENGTH_SHORT).show();

            public void onNext(Integer integer) {
                Toast.makeText(MainActivity.this, "onNext called: " + integer, Toast.LENGTH_SHORT).show();

            public void onError(Throwable e) {
                Toast.makeText(MainActivity.this, "onError called", Toast.LENGTH_SHORT).show();

            public void onComplete() {
                Toast.makeText(MainActivity.this, "onComplete called", Toast.LENGTH_SHORT).show();

3. Subscribing Observer to Observable

This subscription can be done in a simple one liner:


Our completed RxAndroid Example Activity:

Now the finished MainActivity.java will look as follows:

Now we can see that whenever our button is clicked, Our Observable emits data and Observer subscribes and receives data.

The Complete code for this tutorial can be downloaded here:


rxandroid example tutorial link


In the next tutorial of RxAndroid, we will see different ways to create Observables and much more!

0 0 vote
Article Rating
Notify of

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

Inline Feedbacks
View all comments