3

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.

1
  • 2
    "What I found in most of the tutorial on the internet" -- check the dates on those tutorials. Jetpack Compose is still in a developer preview right now, and that means that the API changes very quickly. Aug 3, 2020 at 11:03

6 Answers 6

9

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

1

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

0

Use blow code

Button(onClick {/* your onClick listener */}){
   Text("I'm a Compose Button")
}
0

I know it does not seems good but u have to do it like this

                Button(onClick = {handle Click Action }) 
                     {
                    Text(text = "Your Button Text")
                }
0

those who are new in compose can use button simple button like this

    @Composable
fun MyButton() {
    Column(
        modifier = Modifier.fillMaxWidth().fillMaxHeight(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(
            onClick = {},
            modifier = Modifier.padding(all = Dp(10f)),
            enabled = true,
            border = BorderStroke(width = 1.dp, brush = SolidColor(Color.Blue))

        ) {
            Text(text = "I am a compose button", color = Color.White)
        }
    }
}
-1

You are not using the syntax,

you the this code:

    Button(onClick = {/*Handle click action */} ){
    Text(
        text = "Write the button text here"
    )
}

if you want to use modifiers then refer to this,

    Button(onClick = {/*Handle click action */}, modifier = Modifier.padding(16.dp)) {
    Text(
        text = "Write the button text here"
    )
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.