When a button is clicked once in kotlin compose, so if the button is clicked, I want it not to be clicked again. Is there a simple way to do this in kotlin compose?
1 Answer
Depends on what you want to achieve - do you want to disable the button after clicking (which changes the style of the button to disabled) or do you simply want to keep the button style the same but not handle any clicks after the first one (without visible UI changes)?
If (1):
var isEnabled by remember {
mutableStateOf(true)
}
Button(
enabled = isEnabled,
onClick = {
if (isEnabled) isEnabled = false
}
) {
...
}
If (2):
var hasButtonBeenClicked by remember {
mutableStateOf(false)
}
Button(
onClick = {
if (!hasButtonBeenClicked) hasButtonBeenClicked = true
}
) {
...
}
If you have a ViewModel, you could also manipulate the enabled values there and simply observe the state changes in your UI.