Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text is not updating on button in Jetpack Compose

I want to change the text that’s appearing on the button each time I click it, so I have written the following code, but it’s not working. Where am I going wrong?

    class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var i=0;
            Button(onClick = {i++ }) {
                Text("Clicked $i times") //!!not updating here
            }
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Check how compose works with states and recomposition.

Use something like:

var i by remember { mutableStateOf(0) }

Button(onClick = {i++ }) {
    Text("Clicked $i times")
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading