Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve double tap on Button Issue in android?

Mockup of my Application :

Double Tap Mockup


Problem :

When click on button1 it just call Intent of ActivitySecond

button1.setOnClickListener(this);

public void onClick(View v) {
// TODO Auto-generated method stub
     switch (v.getId()) 
     {
          case R.id.button1:
                Intent intent = new Intent(getApplicationContext(), ActivitySecond.class);
                startActivity(intent);
                break;
          default:
                break;
     }
}

But, on Double tap it open twice ActivitySecond.


HOW TO RESOLVE IT.

PLEASE IF ANY SOLUTION THEN SHARE IT.

Thank you.

like image 830
Darshak Avatar asked May 21 '13 05:05

Darshak


People also ask

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.

How do you double click on an Android phone?

To switch the behavior of a single click between left click and right click, tap the Mouse button. A double tap acts as a double click. A double tap and hold allows you to grab and then drag. A two-finger tap acts as a right click.

What is the double tap button?

1. Double-click is a term used to describe the process of quickly pressing a mouse button twice while keeping it still. In most cases, a double-click is with the left mouse button and is used to open or execute a file, folder, or software program.


4 Answers

btn.setOnclickListener(new View.onClickListener(){

          public void onClick(View v) {
                btn.setEnabled(false);

          }
    });

you have to make the setEnabled(false) in onlclick event.

like image 155
Google Avatar answered Oct 24 '22 13:10

Google


As Gabe Sechan sad:

This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click)

Here is an implementation that i used in my project:

public abstract class OnOneClickListener implements View.OnClickListener {
    private static final long MIN_CLICK_INTERVAL = 1000; //in millis
    private long lastClickTime = 0;

    @Override
    public final void onClick(View v) {
        long currentTime = SystemClock.elapsedRealtime();
        if (currentTime - lastClickTime > MIN_CLICK_INTERVAL) {
            lastClickTime = currentTime;
            onOneClick(v);
        }
    }

    public abstract void onOneClick(View v);
}

Just use OnOneClickListener instead of OnClickListener and execute your code in onOneClick() method.

The solution with disabling button in onClick() will not work. Two clicks on a button can be scheduled for execution even before your first onClick() will execute and disable the button.

like image 35
Yaroslav Buhaiev Avatar answered Oct 24 '22 12:10

Yaroslav Buhaiev


This is called debouncing- its a classical problem in hardware and in software. There's a couple of tricks you can do, but they all boil down to disabling the button temporarily and re-enabling it later. This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click). Another way would be to disable the button after onClick and re-enable it when the new Activity finishes via onActivityResult. Or there's a dozen other ways, pick the easiest for you.

like image 29
Gabe Sechan Avatar answered Oct 24 '22 11:10

Gabe Sechan


You can set launchMode of ActivitySecond to singleTop

<activity android:name=".ActivitySecond"
            android:launchMode="singleTop"
            >
            ...
</activity>
like image 27
Glenn Avatar answered Oct 24 '22 12:10

Glenn