7

How to implement double click event in android without using gesturedetector?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user386430
  • 4,837
  • 13
  • 41
  • 45

5 Answers5

8

If you mean double tap you have to use GestureDetector.OnDoubleTapListener.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
2

I'm sure all the code there does is determine if the second click was within a certain time of the first click, otherwise treat it as a second click. That's how I would do it anyway.

1

just use setOnTouchListener to record the first and second click time. If they are very close, determine it as a double click. Like this,

public class MyActivity extends Activity {

    private final String DEBUG_TAG= "MyActivity";
    private long firstClick;
    private long lastClick;
    private int count; // to count click times

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button mButton= (Button)findViewById(R.id.my_button);
        mButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // if the second happens too late, regard it as first click
                        if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
                            count = 0;
                        }
                        count++;
                        if (count == 1) {
                            firstClick = System.currentTimeMillis();
                        } else if (count == 2) {
                            lastClick = System.currentTimeMillis();
                            // if these two clicks is closer than 300 millis second 
                            if (lastClick - firstClick < 300) {
                                Log.d(DEBUG_TAG,"a double click happened");
                            }
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return true;
            }
        });
    }
}
wuliang8910
  • 271
  • 3
  • 4
1

Look here, this is library in jar for listening touch gestures, implement and work ) https://github.com/NikolayKolomiytsev/zTouch

0

Look at the source code for GestureDetector and copy the bits you need (specifically, look at the isConsideredDoubleTap method)

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/GestureDetector.java

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
oli
  • 4,894
  • 1
  • 17
  • 11
  • i am using chart application if i click the point for double click it will go to another activity.if i am used gesture detector if i click anywhere it will go to other activity – user386430 Jul 16 '10 at 06:37