Android Jetpack Compose – Button

Android Compose – Button

To display a button in Android UI, you can use Button composable.

Button(onClick = {  }) {
    Text(text = "Click Me")
}

onClick parameter is used to define the action that can be performed when user clicks or presses on the button.

Inside the Button, you can use a Text composable to display the text in the button.

Example Android application with Button composable

Let us display a Button in the Android UI, using Button composable.

MainActivity.kt

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Column(
                    modifier = Modifier.fillMaxWidth().padding(vertical = 20.dp),
                    horizontalAlignment = Alignment.CenterHorizontally) {
                        Button(onClick = { }) {
                            Text(text = "Click Me")
                        }
                }
            }
        }
    }
}

Screenshot

Android Compose - Button - Example