Last Updated: February 25, 2016
· sisardor

Prevent double click on Android buttons

Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.

private long lastClickTime = 0;

View.OnClickListener buttonHandler = new View.OnClickListener() {
    public void onClick(View v) {
        // preventing double, using threshold of 1000 ms
        if (SystemClock.elapsedRealtime() - lastClickTime < 1000){

        lastClickTime = SystemClock.elapsedRealtime();

2 Responses
Add your response

To prevent this, you can use ProgressDialog with attribute cancelable(false)

over 1 year ago ·

NO. ProgressDialog is not perfect enough. Android queue up the click event. So two ProgressDialogs may show up when click fast enough.

over 1 year ago ·