0

I'm currently creating a custom double tap using the onClickListener with the following code:

newImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
counterTap++;

Timer t = new Timer("Double tap counter");
t.schedule(new TimerTask() {

@Override
public void run() {
counterTap = 0;
}
}, 0, 300);

if(counterTap >= 2) {
newImage.setVisibility(FrameLayout.GONE);
counterTap = 0;
}

}
});

The problem I'm facing is as follows:

Whenever I tap the ImageView the event does fire. However, the second time I tap the ImageView, the above code only executes when clicking on the exact same position on the ImageView as before.

James
  • 1,036
  • 1
  • 8
  • 24
  • Why can't you just use the Android GestureDetector? – Blackbelt Mar 19 '13 at 11:37
  • @blackbelt is right. Use a GestureDetector. There is already a method that handles double tap – the-ginger-geek Mar 19 '13 at 11:52
  • I tried using the GestureDetector as well but I'm facing the exact same problem. It seems that the second tap has to be on the exact same position as the first tap, even though I added the listener to an ImageView. – James Mar 19 '13 at 12:35

1 Answers1

2

Rather use a onTouchListener. In the touch listener in the onTouch method you can return false the first time you tapped which means the event was not handled, the second time you return true and the touch listener will handle the event as finished. See my example below, you can use a similar method

new OnTouchListener() {
    boolean doubleTap = false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            if (!doubleTap) {
                doubleTap = true;
                return false;
            } else {
                return true;
            }
        }
    }
};

this might solve your issue.

EDIT : This is a better opotion

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    // double tap event
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return true;
    }
}

This question might also help you get to the best answer

Community
  • 1
  • 1
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
  • I'll give that onTouch a try. I'm facing the exact same problem with the GestureListener's onDoubleTap though. The second tap has to be on the exact same location as the first tap. I want te user to be able to doubleTap anywhere on the ImageView. Picture to clarify what I mean: [CLICK](http://i.imgur.com/LUsmopG.png) When the user taps on position 1 and then on position 2, it won't register the doubletap. – James Mar 19 '13 at 12:42
  • I see Instagram works like that. You have to double tap on the same position. I think it might be better practice to do it that way. Good luck – the-ginger-geek Mar 19 '13 at 12:45