Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Preventing Double Click On A Button

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

like image 396
Androider Avatar asked Oct 18 '22 02:10

Androider


People also ask

How do I stop my keys from double-clicking?

Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.

How does Android handle multiple click events?

You can implement onClickListener in your Activity. Set onClickListener for each button. Then just detect the corresponding button pressed. public class YourActivity extends Activity implements OnClickListener { Button button1, button2; @Override protected void onCreate(Bundle savedInstanceState) { super.

How do we double-click the button?

To double-click, press your left mouse button twice quickly.


1 Answers

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
    }
}
like image 185
qezt Avatar answered Oct 22 '22 08:10

qezt