Using Java’s Lambda Expressions in Your Android App - dummies

Using Java’s Lambda Expressions in Your Android App

By Barry A. Burd

Sometimes, if you open code in Android Studio’s editor and hover the mouse over certain words, you get an interesting surprise. Android Studio tells you that you can replace the anonymous inner class with a lambda expression.

java-programming-for-android-developers-2e-lambda-expressions
You can create a lambda expression.

Okay. What’s a lambda expression? For starters, lambda is a letter in the Greek alphabet, and the term lambda expression comes from papers written in the 1930s by mathematician Alonzo Church.

In 2013, Oracle released Java 8, adding lambda expressions to the Java language. And in 2016, Google made Java 8 features available to Android developers.

What is a lambda expression exactly? A lambda expression is a concise way of declaring an interface that contains only one method. For example, an anonymous OnClickListener could have only one method, namely, the onClick method. So you can replace this anonymous OnClickListener with a lambda expression.

If you respond to the message by pressing Alt+Enter, Android Studio offers you a Replace with Lambda option. If you accept this option, Android Studio turns your code into this stuff.

package com.allmycode.a11_05;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button button;

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button);

button.setOnClickListener(view -> textView.setText(R.string.you_clicked));

textView = (TextView) findViewById(R.id.textView);

}

}

This code uses a lambda expression. The image illustrates the transition from a class that implements a one-method interface to a lambda expression.

java-programming-for-android-developers-2e-interface-into-lambda
Turning an interface into a lambda expression.

Notice the lightweight role of the word view. When you declare an onClick method, you give the method a parameter of type View even if the statements inside the method don’t use that parameter. In the same way, when you create a lambda expression for an onClick method, you preface the -> symbol with a parameter name, even if you don’t use that parameter name to the left of the -> symbol.

In order to use lambda expressions, you must satisfy certain requirements. For example, you must compile your code with Java 8 or higher. Your Android Studio version must be 2.1 or higher. And your project’s build.gradle file must include the following code:

android {

...

defaultConfig {

...

jackOptions {

enabled true

}

}

...

}

A lambda expression may have more than one parameter to the left of the -> symbol. If it does, you must enclose all the parameters in parentheses and separate the parameters from one another with commas. For example, the expression

(price1, price2) -> price1 + price2

is a valid lambda expression.

If you’re comfortable with lambda expressions, you can make your code much more readable. What started out as about ten lines of code can easily become only part of a line.