Search code examples
androidbuttonclickable

A button clickable when conditions are met (Android Studio)


Is there a feature that I can make a button clickable only when a certain condition is met?

For instance, I can ask users to swipe 10 times, and only after users swipe 10 times, a button is clickable to receive an award.

Thank you for your help in advance!


Solution

  • Disable the button (instead of disabled you can set isFocused = false):

    button.isClickable = false
    button.isEnabled = false
    

    Add swipe-listener. Inside it increment a counter. After it becomes 10, set:

    button.isClickable = true
    button.isEnabled = true
    

    Also inside a click-listener you can check the counter >= 10.