3

I am currently using dispatchTouchEvent to grab touch events, is there an easy way to distinguish between a click and a "drag" stylt gesture?

Jackie
  • 21,969
  • 32
  • 147
  • 289

2 Answers2

1

DispatchTouchEvent is called with a MotionEvent parameter. Method getAction within MotionEvent can return

  • ACTION_DOWN
  • ACTION_MOVE
  • ACTION_UP
  • ACTION_CANCEL

Then set on ACTION_DOWN flag isClick. If there is ACTION_MOVE clear isClick flag.

switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        isClick = true;
        break;
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (isClick) {
            //TODO Click action
        }
        break;
    case MotionEvent.ACTION_MOVE:
        isClick = false;
        break;
    default:
        break;
    }
    return true;
}
Aman Jham
  • 478
  • 6
  • 18
MSquare
  • 6,311
  • 4
  • 31
  • 37
  • 2
    Seems like no matter how brief I tap, the action move event always get's fired. – JeffRegan Jun 26 '14 at 19:18
  • Check my other answer which introduces a movement threshold before setting isClick to false: http://stackoverflow.com/a/16485989/1373248 – MSquare Jun 27 '14 at 10:27
  • I implemented a similar solution yesterday. You might want to update this answer. – JeffRegan Jun 27 '14 at 14:43
  • This will not reliably work because small twitches in the finger will trigger `ACTION_MOVE` thus nullifying your click action. – user2968401 Sep 01 '15 at 03:18
  • @user2968401: True! You cam implement move threshold : http://stackoverflow.com/questions/4324362/detect-touch-press-vs-long-press-vs-movement/16485989#16485989 – MSquare Sep 01 '15 at 08:31
0

set up a threshold limit.When you move the pointer within a small range make it recognized as a click or else a movement