fargoh fargoh - 1 year ago 73
Android Question

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

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?

Answer Source

In your onClickListener for the button:


Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {

    public void run() {
        runOnUiThread(new Runnable() {

            public void run() {
}, 5000);

This will disable the button when clicked, and enable it again after 5 seconds.

If the click event is handled in class that extends View rather than in an Activity do the same thing but replace runOnUiThread with post.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download