How to prevent rapid double click on a button
I use a function like this in the listener of a button:
public static long lastClickTime = 0;
public static final long DOUBLE_CLICK_TIME_DELTA = 500;
public static boolean isDoubleClick(){
long clickTime = System.currentTimeMillis();
if(clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
lastClickTime = clickTime;
return true;
}
lastClickTime = clickTime;
return false;
}
I am doing like this it works very well.
public abstract class OnOneOffClickListener implements View.OnClickListener {
private static final long MIN_CLICK_INTERVAL=600;
private long mLastClickTime;
public static boolean isViewClicked = false;
public abstract void onSingleClick(View v);
@Override
public final void onClick(View v) {
long currentClickTime=SystemClock.uptimeMillis();
long elapsedTime=currentClickTime-mLastClickTime;
mLastClickTime=currentClickTime;
if(elapsedTime<=MIN_CLICK_INTERVAL)
return;
if(!isViewClicked){
isViewClicked = true;
startTimer();
} else {
return;
}
onSingleClick(v);
}
/**
* This method delays simultaneous touch events of multiple views.
*/
private void startTimer() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
isViewClicked = false;
}
}, 600);
}
}