18

How can I implement double click for a button in Android? Should I use OnDoubleTapListener?

1

7 Answers 7

34
int i = 0; btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub i++; Handler handler = new Handler(); Runnable r = new Runnable() { @Override public void run() { i = 0; } }; if (i == 1) { //Single click handler.postDelayed(r, 250); } else if (i == 2) { //Double click i = 0; ShowDailog(); } } }); 
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't take into account any timing constraints.
@ParagChauhan please take a look at this question
Please fix this post for those who have OCD
This code executes Single click event for Double Click as well.
5
private long lastTouchTime = 0; private long currentTouchTime = 0; 

..

 @Override public void onClick(View view) { lastTouchTime = currentTouchTime; currentTouchTime = System.currentTimeMillis(); if (currentTouchTime - lastTouchTime < 250) { Log.d("Duble","Click"); lastTouchTime = 0; currentTouchTime = 0; } } 

Comments

4

This is probably a good place to start:

Android: How to detect double-tap?

I recommend switching to a more native way like long press (answer to linked question) or something more creative (using multi-touch), unless you are bent on the Windows default double-click way of doing things?

You may have a valid reason though - double clicking is after all faster than long press.

Comments

4

I wrote this for popping up a Toast message on a double click in a mapping application:

private long lastTouchTime = -1; @Override public boolean onTouchEvent(MotionEvent e, MapView mapView) { GeoPoint p = null; if (e.getAction() == MotionEvent.ACTION_DOWN) { long thisTime = System.currentTimeMillis(); if (thisTime - lastTouchTime < 250) { // Double click p = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY()); lastTouchTime = -1; } else { // too slow lastTouchTime = thisTime; } } if (p != null) { showClickedLocation(p);// Raise a Toast } return false; } 

Comments

3

This is a good site for performing double click... I used it and worked.

http://mobile.tutsplus.com/tutorials/android/android-gesture/

Comments

1

I used it and worked:

public class DoubleClickTest extends Activity { String TAG = "DoubleOrSingleClickTest"; private boolean waitDouble = true; private static final int DOUBLE_CLICK_TIME = 350; // double click timer @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.double_click_test); Button button = (Button) findViewById(R.id.buttonDoubleOrSingleClicked); button.setOnClickListener(listenerDoubleOrSingle); } View.OnClickListener listenerDoubleOrSingle = new View.OnClickListener() { @Override public void onClick(View v) { if (waitDouble == true) { waitDouble = false; Thread thread = new Thread() { @Override public void run() { try { sleep(DOUBLE_CLICK_TIME); if (waitDouble == false) { waitDouble = true; singleClick(); } } catch (InterruptedException e) { e.printStackTrace(); } } }; thread.start(); } else { waitDouble = true; doubleClick(); } } }; // single event private void singleClick() { Log.i(TAG, "singleClick"); } // double event private void doubleClick() { Log.i(TAG, "doubleClick"); } } 

It comes from "https://elingwange.iteye.com/blog/1613177"

Comments

0

Create your own DoubleTapListener

You can create a DoubleTapListener by inheriting View.OnClickListener and adding a Callback of your listener.

MyDoubleClickListener.class

public class MyDoubleClickListener implements View.OnClickListener{ private boolean isRunning= false; private int resetInTime =500; private int counter=0; private DoubleClickCallback listener; public DoubleTapListener(Context context) { listener = (DoubleClickCallback)context; } @Override public void onClick(View v) { if(isRunning) { if(counter==1) //<-- makes sure that the callback is triggered on double click listener.onDoubleClick(v); } counter++; if(!isRunning) { isRunning=true; new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(resetInTime); isRunning = false; counter=0; } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } } 

DoubleClickCallback.class

public interface DoubleClickCallback { public void onDoubleClick(View v); } 

And you are done. You can use this Listener in any Activity.

How do I use this DoubleClickListener in my Activity?

Implement Callback in your activity and override the method.

public class MainActivity extends AppCompatActivity implements MyDoubleClickListener{ private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new DoubleTapListener(this)); //<-- Set listener } @Override public void onDoubleClick(View v) { // Toast to show double click } } 

Important point is using this concept you can create any kind of listener (Triple-click listener)

Relevant Links:

See the full working code HERE

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.