Implement double click for button in Android

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


    }
});

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

                }

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.


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

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

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