androidkotlinandroid-jetpack-compose

android-jetpack-compose after click button I want to disable button for some time and enable it again


like title said, I need after click to disable button for some time, so it will not be clickable, and when that time expired to enable again button so it can be clickable.

    Button(
      onClick = {},
      modifier = //maybe to add code here?
    ) {
        Text(text = "Click me")
      }

It would be great if code can be added to Modifier, that would be great! Because then it would been reusable and used not just for Button, and for others components too.


Solution

  • Here's one way of achieving this with delay:

        var enabled by remember { mutableStateOf(true) }
    
        LaunchedEffect(enabled) {
            if (enabled) return@LaunchedEffect
            else delay(1000L)
            enabled = true
        }
    
        Button(
            onClick = { enabled = false }, 
            enabled = enabled
        ) {
            Text(text = "Click Me")
        }