Cannot create Button in Jetpack Compose

Viewed 3621

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.

6 Answers

Use blow code

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

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")
                }

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)
        }
    }
}

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"
    )
}
Related