I have looked at the answers here - Android Preventing Double Click On A Button
and implemented qezt's solution like and I've tried setEnabled(false)
like so -
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mis-clicking prevention, using threshold of 1 second
if (SystemClock.elapsedRealtime() - doneButtonClickTime < 1000){
return;
}
//store time of button click
doneButtonClickTime = SystemClock.elapsedRealtime();
doneButton.setEnabled(false);
//do actual work
}
});
Neither of these work against super fast double clicks.
Note - I'm not setting doneButton.setEnabled(true)
after my processing is done. I simply finish() the activity so there is no issue of the button getting enabled too soon.
To prevent multiple button clicks in React: Set an onClick prop on the button, passing it a function. When the button gets clicked, set its disabled attribute to true .
The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example: // Make your activity class to implement View. OnClickListener public class MenuPricipalScreen extends Activity implements View.
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.
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);
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With