0

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 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.