1

My app has a View which is clickable, and then when this View is clicked, a handleClickEvent() should be called. I tried setting an OnClickListener to the view, but then when user is double tapping the view, the handleClickEvent() gets called twice. I don't want it to be called twice during double tap. I.e. I want handleClickEvent() be called ONCE for both single click and double tap.

I did some research and found out this question: Android detecting double tap without single tap first I tried what's suggested in the answer, i.e. implementing a GestureDetector with onSingleTapConfirmed() method. That worked, however I notice that there is a noticeable delay on the response time, comparing to using OnClickListener. I assume it is because onSingleTapConfirmed() is called only after the system confirms that this is a single tap by waiting for some certain time period.

Is there anyway to achieve this without having a noticeable delay? Another option I can think of is to have a lastClickedTimeStamp member variable, compare the current time when clicked with lastClickedTimeStamp, and if it is within some threshold don't do anything.

I am wondering if there are any other options?

Thank you!

Community
  • 1
  • 1
HeyThere
  • 503
  • 1
  • 5
  • 19

2 Answers2

2

The way I would do it is set a state variable as a long and call it timeOfLastTouch and set it to 0. Then in each OnClickListener function put this code in their

long currentTime = System.currentTimeMillis();
if (timeOfLastTouch + 100 < currentTime) {
    // code
    timeOfLastTouch = System.currentTimeMillis();
}
Cameron Monks
  • 107
  • 1
  • 7
  • Thank you! Yes I think this will work. I was just wondering if there is anything built in that could do it. If not I'll go ahead with this approach. – HeyThere Apr 17 '17 at 03:12
0

You can try using GestureDetector.SimpleOnGestureListener and combine it with onTouchEvent.

private GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.d("SingleTap", " is detected");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.d("DoubleTap", " is detected");
        return true;
    }
});

then, remove onClickListener and set touch event on that view

 yourView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return gestureDetector.onTouchEvent(event);
    }
});

You can refer to Detecting Common Gestures

Amad Yus
  • 2,856
  • 1
  • 24
  • 32
  • As I mentioned in the question, I already tried this approach. It works but there is a noticeable delay between user click the view and the handleClickEvent() being called. Hence I am looking for an alternative approach if possible. Thanks! – HeyThere Apr 17 '17 at 03:10
  • My bad. I didn't notice the link above. If that is the case, why don't you set the view to clickable(false) when handleClickEvent is being called and set it to true when you finish? Much simpler that way. – Amad Yus Apr 17 '17 at 03:23