Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent rapid double click on a button

Tags:

android

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.

like image 351
Mallika Khullar Avatar asked Sep 28 '15 09:09

Mallika Khullar


People also ask

How do I stop multiple clicks on a button?

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 .

How do you avoid multiple buttons clicking at the same time in flutter?

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.

How do I stop a button from clicking multiple times android?

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.


2 Answers

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);

    }

}
like image 76
Suru Avatar answered Oct 12 '22 06:10

Suru


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;
}
like image 34
Jean-baptiste Valero Avatar answered Oct 12 '22 07:10

Jean-baptiste Valero