Thomas Kioko

Thomas Kioko

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:

PREVIOUS POST

NEXT POST

2 Comments

Hey! I just stumbled with your page! it’s awesome! I’ve been using this in methods like : .setAction(R.string.retry, v -> fetchPayments()) and I had no idea why it just WORKS, I decided to look it up and stumbled upon your page, will be reading all your articles, thanks man!