I have a button and when it is pressed it plays an audio file. I want to put a 5 second delay on the button so users wont mash the button and play the sound over and over. I guess what i really want it for the button to be disabled for 5 seconds after it is pushed. Does anyone know how to do this?
You can disable your button, then use the postDelayed method on your button.
myButton.setEnabled(false);
myButton.postDelayed(new Runnable() {
@Override
public void run() {
myButton.setEnabled(true);
}
}, 5000);
This is similar to the Timer solution, but it might better handle configuration change (for example if the user rotate the phone)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With