Open In App

Pass an On-Click Event to a Function in Android Jetpack Compose

Last Updated : 14 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Jetpack Compose, we can create functions to create any type of UI element. Suppose, we create a function to create a Button, we can use it multiple times to create multiple buttons. While creating multiple Buttons, we can add functionality to them to perform any given task, which we call On-Click Event. So in this article, we will show you how you could create such a function and pass an On-Click Event to another Function in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcpassonclicktofunction
 
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
 
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display element and its contents
            MainContent()
        }
    }
}
 
// Creating a composable
// function to display Top Bar
@Composable
fun MainContent(){
    Scaffold(
        topBar = {TopAppBar(
            title = {Text(
                "GFG | Change Button Size",
                color = Color.White)},
            backgroundColor = Color(0xff0f9d58)
        ) },
        content = { MyContent()}
    )
}
 
// Creating a composable function to create
// three buttons and add functionality to them
// to add or subtract from the counter value
// Calling this function as content in the above function
@Composable
fun MyContent() {
 
    // Declaring a value to store the counter value
    // Initially this value is set to 0
    val mCounter = remember { mutableStateOf(0)}
 
    Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
         
        // Displaying the mCounter value as Text
        Text(text = mCounter.value.toString(), fontSize = 50.sp)
 
        // Adding a space of 50dp
        Spacer(modifier = Modifier.height(50.dp))
 
        // Calling CreateButton function to create a button,
        // on click will increase counter value by 5
        CreateButton(text = "Add 5") {
            mCounter.value += 5
        }
 
        // Adding a space of 50dp
        Spacer(modifier = Modifier.height(50.dp))
 
        // Calling CreateButton function to create a button,
        // on click will increase counter value by 10
        CreateButton(text = "Add 10") {
            mCounter.value += 10
        }
 
        Spacer(modifier = Modifier.height(50.dp))
 
        // Calling CreateButton function to create a button,
        // on click will decrease counter value by 5
        CreateButton(text = "Subtract 5") {
            mCounter.value -= 5
        }
    }
}
 
// Creating a composable
// function to create a button
@Composable
fun CreateButton(text:String, onClick: ()-> Unit){
    Button(
        onClick = onClick,
        colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58))
    ){
        Text(text = text, color = Color.White)
    }
}
 
// For displaying preview in the
// Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


 

 

Output:

 

The following video is the working of our application. You can see that All Buttons are functional and change the Counter value on-click.

 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads