11

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.

Darshak
  • 2,298
  • 4
  • 23
  • 45
  • 2
    synchronize the onClick, and disable your button by `setEnabled(false)` on first click, and `setEnabled(true)` onResume – Chor Wai Chun May 21 '13 at 05:34
  • Answer has been posted here [prevent double tap on button android](https://stackoverflow.com/questions/11290610/how-to-prevent-double-code-running-by-clicking-twice-fast-to-a-button-in-android/45917595#45917595) – anand krish Aug 28 '17 at 12:12

6 Answers6

13

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.

Yaroslav Buhaiev
  • 1,187
  • 11
  • 16
9

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
6

You can set launchMode of ActivitySecond to singleTop

<activity android:name=".ActivitySecond"
            android:launchMode="singleTop"
            >
            ...
</activity>
Glenn
  • 12,741
  • 6
  • 47
  • 48
  • 6
    Not the best solution IMO, as it leads to bad user experience. The second activity still starts twice (which is noticed by user unless animation is disabled and is suboptimal in terms of resource consumption). Also, setting this flag might mess with the navigation. – Andrii Chernenko May 21 '13 at 06:13
3
btn.setOnclickListener(new View.onClickListener(){

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

          }
    });

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

Google
  • 2,183
  • 3
  • 27
  • 48
  • I don't want to set enable/disable, its lengthy process for me because I have 200+ layouts in my application. Your code is also true. – Darshak May 21 '13 at 05:46
0

I will just submit a third solution for this. I solved it by adding a boolean which was used to check if the activity (which the button starts) has been started or not.

7heViking
  • 7,137
  • 11
  • 50
  • 94
-1

When I use selector drawable in Buttons and set

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

it performs the onClick() event on double click. I found it accidentally to be working on android emulator api level 10, Android 2.3.3 Didn't tested on real device. Here is the Complete code.

               <Button
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Discover"
                android:id="@+id/Button1"
                android:layout_weight=".5"
                android:layout_margin="0dp"
                android:background="@drawable/btn_nearby"
                android:contentDescription="gjhfjhkjhgkvkjh"
                android:drawableLeft="@drawable/ic_follow"
                android:paddingLeft="20dp"
                android:paddingRight="0dp"
                android:drawablePadding="-10dp"
                android:textSize="16sp"
                android:paddingTop="2.5dp"
                android:paddingBottom="2.5dp"
                android:clickable="true"
                android:focusable="true"
                android:focusableInTouchMode="true"/> 

And Java Code

    @Override
    public void onClick(View view) {
        switch(view.getId()) {

            case R.id.Button1:
                onButton1Click();
                break;

            case R.id.Button2:
                onButton2Click();
                break;

          }
    }
Ali Azhar
  • 1,703
  • 19
  • 14