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.

2
  • 2
    synchronize the onClick, and disable your button by setEnabled(false) on first click, and setEnabled(true) onResume Commented May 21, 2013 at 5:34
  • Answer has been posted here prevent double tap on button android Commented Aug 28, 2017 at 12:12

6 Answers 6

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can u please resolve my similar query .. i am trying to raise a toast when power button is pressed twice
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.

Comments

6

You can set launchMode of ActivitySecond to singleTop

<activity android:name=".ActivitySecond" android:launchMode="singleTop" > ... </activity> 

1 Comment

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.
3
btn.setOnclickListener(new View.onClickListener(){ public void onClick(View v) { btn.setEnabled(false); } }); 

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

2 Comments

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.
setEnable(false) would be the obvious solution , but , surprise , it doesn't solve the fast double tap...
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.