What I found in most of the tutorial on the internet is:
@Composable
fun addButton() {
Button(text = "I'm a Compose Button")
}
But android studio give me an error: type mismatch Required: () -> Unit, Found: String. I don't know how to fix this.
According to the docs, To create a Button you have to specify the text inside the RowScope:
Button(onClick = {/* To execute when button is clicked */}) {
Text("I'm a Compose Button")
}
The text is then set by Text() function defined in androidx.ui.foundation package.
You can find more about them in the material-ui docs: https://developer.android.com/reference/kotlin/androidx/ui/material/package-summary
This is how you can add Button in Jetpack compose.
Button(onClick = {/*Handle click action */}, modifier = Modifier.padding(16.dp)) {
Text(
text = "Jetpack Compose Button"
)
}
References:
[1] https://developer.android.com/reference/kotlin/androidx/compose/material/package-summary#button
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With