Introduction
A Button lets users tap to do something, like sending a message or opening a screen.
A Button lets users tap to do something, like sending a message or opening a screen.
Button(onClick = { /* action here */ }) {
Text("Button Label")
}The onClick is what happens when the user taps the button.
The content inside the button is usually a Text showing the label.
Button(onClick = { println("Clicked") }) {
Text("Click me")
}Button(onClick = { /* do something */ }) {
Text("Submit")
}Button(onClick = { /* open screen */ }) {
Text("Next")
}This program shows a button labeled "Press me". When tapped, it prints "Button pressed" in the console.
import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.tooling.preview.Preview @Composable fun SimpleButton() { Button(onClick = { println("Button pressed") }) { Text("Press me") } } @Preview @Composable fun PreviewSimpleButton() { SimpleButton() }
Buttons should have clear labels so users know what will happen.
You can customize buttons with colors and shapes using parameters.
Use enabled = false to disable a button so it can't be tapped.
Buttons let users tap to do actions.
Use Button(onClick = { }) with a Text label inside.
Always give buttons clear and simple labels.
Button composable in Android Jetpack Compose?Button(onClick = { println("Tapped") }) {
Text("Press Here")
}Button(onClick = println("Clicked")) {
Text("Tap")
}
What is the error?enabled = false after first click correctly uses state to disable button after click. Others ignore state or misuse enabled.Pass the test and this pattern moves unknown