Posts Tagged


RxJava + Android

In the previous post we talked about  Lambdas on Android.

In this article, we are going to explore the amazing world of  Reactive Programming.  This is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. Chances are you’ve heard of RxJava. RxJava is a Java VM implementation of ReactiveX (Reactive Extensions): a library for composing asynchronous and event-based programs by using observable sequences.

Why Consider RxJava?

As a developer you don’t have to worry too much about the details of how to perform operations that should occur on different threads. RxJava simplifies development because it raises the level of abstraction around threading. If you have done threading, it can be a nightmare. If not correctly implemented, can cause some of the most difficult bugs to debug and fix.



So let’s say you want to compare if two passwords match. We’d need to implement .addTextChangedListener() to listen for any changes on the EditText then compare if the passwords match.



This how the above would look.

Let’s Go Rx.

1. The Observable. We’ll implement an Observable which will emit changes. An Observable is the stream abstraction in RxJava. It is similar to an Iterator in that, given a sequence, it iterates through and produces those items in an orderly fashion.

Observable passwordObservable = RxTextView.textChanges(mPasswordEditText);

2. Subscription The Subscribe operator is the glue that connects an observer to an Observable. In order for an observer to see the items being emitted by an Observable. In English, the Subscription will replace .addTextChangedListener().

.observeOn() : This will allow us to specify where we want us to run the operation, in this case main thread.

.debounce(): This will enable to wait for x time before comparing items.

.doOnNext(): We’ll use this to hide the error if the password is correct. Putting it all together

3. Observable.combineLatest()

This combines the latest item emitted by each Observable and helps us compare the observables.



If the passwords match, the error message on confirm password is removed & register button is enabled.

Final Result



The source code is available on GitHub

In the next series we’ll implement Realm… Go forth an Rx-ify the world.


Lambdas on Android – Part 1

This is a four part series of how to setup and user Lambda Expressions in Android. In Part 2, we’ll use Lambda expressions with RxJava.

If you are tired of writing boilerplate code, then you should try out Java 8. Let’s write some code.


Enabling Lambda Feature on Gradle

We need to edit your build.gradle file


Implementing Lambdas

We can now write shorter code. In this example we’ll implement OnClickListener on a button to demonstrate this. The old way of doing this looks like this:Now with Lambdas.  

Method Reference

Lambda expressions contain double colons :: . These refer to a new syntax in Java 8 known as method references. You can reference a class or instance and pass along the method that will handle the event:

It’s that simple. Source code is available on GitHub.


Known issues with using the Jack toolchain

  • Instant Run does not currently work with Jack and will be disabled while using the new toolchain.
  • There’s no support for Data Binding
  • Annotation support for libraries such as Dagger 2 to show you in your Android project may not work with Jack. The most recent experimental releases are starting to add this support.

The work-around for this is using Retrolambda instead.

That’s it for now



In Part 2, we’ll use Lambda expressions with RxJava. Happy coding.