RxJava: convert a listener into an Observable

Reading time ~1 minute

In Java it’s common to work with listeners. And if you work with RxJava, you will prefer to use an Observable instead of listeners. But what if you have to deal with a library and you cannot change the source code?

There’s a simple way to create an Observable around a listener.

Suppose we have an interface ValueUpdateListener and an object ValueUpdater that will call our listener:

public interface ValueUpdateListener {
    
    void onValueChanged(@NonNull String value);

}

public class ValueUpdater {
    
    // in some point of the class...
    
    public void registerListener(ValueUpdateListener listener) {
        //...
    };
    
    public void unregisterListener(ValueUpdateListener listener) {
        //...
    };
}

We can create an Observable like this:

public Observable<String> observableListenerWrapper() {

    return Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            ValueUpdateListener listener = new ValueUpdateListener() {

                @Override
                public void onValueChanged(@NonNull String value) {
                    if (subscriber.isUnsubscribed()) {
                        registerListener.unregisterListener(this);
                    } else {
                        subscriber.onNext(value);
                    }
                }
            };

            registerListener.registerListener(listener);
        }
    });
}

Now you just have to subscribe:

observableListenerWrapper().subscribe(value -> {
    // do something with the new value
});

Note that the listener will be automatically unregistered if the method onValueChanged is called and the observable is unsubscribed.

Android Auto Fit TextView

Android framework provides no support for creating a TextView that can fit its content to its size.There are some libraries out there tha...… Continue reading

Workshop su Google, Android e IoT

Published on June 18, 2016

A test rule for setting device locale

Published on May 09, 2016