ojl6uw
Last Updated: February 25, 2016
·
5.896K
· sisardor
Ac6144a2418ba3d7e3f199e5910abcda

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){
            return;
        }

        lastClickTime = SystemClock.elapsedRealtime();
    }
}
Say Thanks
Respond

2 Responses
Add your response

15848
1adec8562d4c435ca72e9b32cd6adcd9

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

over 1 year ago ·
21666
None

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 ·