Android Preventing Double Click On A Button

Androider picture Androider · Apr 10, 2011 · Viewed 147.2k times · Source

What is the best way to prevent double clicks on a button in Android?

Answer

qezt picture qezt · May 13, 2013

saving a last click time when clicking will prevent this problem.

i.e.

private long mLastClickTime = 0;

...

// inside onCreate or so:

findViewById(R.id.button).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // mis-clicking prevention, using threshold of 1000 ms
        if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
            return;
        }
        mLastClickTime = SystemClock.elapsedRealtime();

        // do your magic here
    }
}