Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create Button in Jetpack Compose

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.

like image 644
Card Reader Avatar asked Jun 17 '26 03:06

Card Reader


2 Answers

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

like image 52
Animesh Sahu Avatar answered Jun 19 '26 16:06

Animesh Sahu


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

like image 34
Rajnish suryavanshi Avatar answered Jun 19 '26 17:06

Rajnish suryavanshi