Reactive Hello world application in android

Published by inkskull on

Reactive Hello world application in android

Adding RxJava 2 library  to a Java project in Android Studio

To use RxJava in a Gradle build, add the following as dependency.

compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.1'

For Maven, you can add RxJava via the following snippet.



Declare an observable data items first

Observable<String> observable = Observable.just("Hello ", " World");


Create an Observer like this

Observer<String> stringObserver = new Observer<String>() {
    public void onSubscribe(Disposable d) {
        Log.d(TAG, "onSubscribe() called with: d = [" + d + "]");

    public void onNext(String s) {
        Log.d(TAG, "onNext() called with: s = [" + s + "]");
        appCompatTextView.setText(String.format("%s%s", appCompatTextView.getText().toString(), s));

    public void onError(Throwable e) {
        Log.d(TAG, "onError() called with: e = [" + e + "]");

    public void onComplete() {
        Log.d(TAG, "onComplete() called");

There Are four methods implement when create an Observer

  • onSubscribe(Disposable d) 
  • onNext(String s) Send a new value to a subscriber
  • onError(Throwable e) Notify observer about an error that has occurred
  • onComplete() Let subscriber know that it should unsubscribe as there won’t be any more content from this observable.

When Subscribing  onSubscribe called first time that observer is subscribed observable data,After that onNext called twice returning string items,When completed items onComplete called and transaction completed. If any error occurs onError called .


Subscribe Observable Using Observer created above.


After Subscribing onNext(String s) called two times  because we put two string items in observable

in onNext(String s)  we setting text to a textview.



Get Full Source code From Github

Thanks Happy coding 😀


Categories: RxJava2

1 Comment

Leave a Reply

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

Social Media Auto Publish Powered By :