Lambda Expressions in Android with Example
Lambda expressions are a feature of Java 8 and later, which can be used in Android development to simplify code and improve readability. They are anonymous functions that can be passed around as values and can be used to create functional interfaces, which are interfaces that have a single abstract method. In Android, lambda expressions can be used to simplify event handling and make the code more readable.
For example, you can set an onClickListener for a button like this:
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // do something } });
With lambda expressions, you can write the same thing like this:
button.setOnClickListener((view) -> { // do something });
Lambda expressions can also be used to simplify the creation of threads or to handle a callback, they can also be used in Android with functional interfaces such as Runnable, Callable, and more.
Syntax in Java and Kotlin
In Java, lambda expressions are used to create functional interfaces, which are interfaces that have a single abstract method. Here’s an example of using a lambda expression to create a functional interface in Java:
Runnable runnable = () -> { // code to run };
In Kotlin, lambda expressions can be used in a similar way, but they have a more concise syntax, and you don’t need to create a functional interface. Here’s an example of using a lambda expression in Kotlin:
val runnable = { // code to run }
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:orientation = "vertical" > <!-- TextView to display the text "Click the Button Below" --> < TextView android:id = "@+id/textView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Click the Button Below" android:textSize = "32sp" /> <!-- Button to perform some action --> < Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Button" /> </ LinearLayout > |
Step 3: Working with the MainActivity & ExampleIntentService File
Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // Declare the button and textView variables private lateinit var button: Button private lateinit var textView: TextView override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) // Set the content view to the layout // defined in activity_main.xml setContentView(R.layout.activity_main) // Find the button and textView in the // layout and bind them to the variables button = findViewById(R.id.button) textView = findViewById(R.id.textView) // Set an OnClickListener for the // button using a lambda expression button.setOnClickListener { // Perform action when the button is clicked textView.text = "Button clicked" } } } |
Java
import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // Declare the button and // textView variables private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // Set the content view to the layout // defined in activity_main.xml setContentView(R.layout.activity_main); // Find the button and textView in the // layout and bind them to the variables button = findViewById(R.id.button); textView = findViewById(R.id.textView); // Set an OnClickListener for the // button using a lambda expression button.setOnClickListener(v -> { // Perform action when the button is clicked textView.setText( "Button clicked" ); }); } } |
Contact Us