Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID: How to put a delay after a button is pushed?

Tags:

android

button

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?

like image 248
fargoh Avatar asked Nov 15 '12 13:11

fargoh


1 Answers

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)

like image 53
nicopico Avatar answered Jan 17 '23 02:01

nicopico