Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack

TextButton's text Not updating


I am making my first app for android in jetpack compose. I am trying to make a text button that will update it's text each time it is pressed, however while it is showing just fine on the device it does not update the Button

@Composable
fun ShotButton(){
    var state by remember { mutableStateOf("N") }
    TextButton(modifier = Modifier.size(60.dp, height = 70.dp),onClick = {
        if(state == "N") state = "Y"
        if(state == "Y") state = "R"
        if(state == "R") state = "N"

    }) {
        Text(text = state, fontSize = 60.sp)
    }
}

I have done many changes to how i declare the button and the state variable, and the documentation doesn't really clear my doubts


Solution

  • You could use when:

    @Composable
    fun ShotButton() {
        var state by remember { mutableStateOf("N") }
        TextButton(modifier = Modifier.size(60.dp, height = 70.dp), onClick = {
            when (state) {
                "N" -> state = "Y"
                "Y" -> state = "R"
                "R" -> state = "N"
            }
        }) {
            Text(text = state, fontSize = 60.sp)
        }
    }