androidmotion-detection

Android MotionEvent: find out if motion happened outside the view


I have a button and OnTouchListener attached to it. How can I find if motion (when user presses the button) happened inside or outside it? Both event.getAction() and event.getActionMasked() return only 0, 1 or 2, which is ActionDown, ActionUp, ActionMove, respectively. There's a constant MotionEvent.ACTION_OUTSIDE, which is 4, but somehow I don't receive it even if I drag touch outside the button - I still receive 2 from both methods. What's the problem?

UPD: I' ve found nice solution - just check focused state on view after ACTION_UP. If it's not focused, it means that movement happened outside the view.


Solution

  • That flag only applies to Windows, not Views. You will get ACTION_MOVE when you move your finger off the View, the event stays in the View it originated with. Look at the source code for SeekBar if you need clarification: even if you move your finger off the bar, the thumb still drags!

    For doing this at the Window level use FLAG_WATCH_OUTSIDE_TOUCH, it works just fine.