Shawn Lien Shawn Lien - 9 months ago 31
Android Question

How to stop receiving the x, y coordinates of motion events when finger leaves the current view in Android?

enter image description here

I have set the

on the blue part of the screen. It will only obtain the x,y coordinates when finger is touched on it. The white part does not do anything at all. The problem is that if at the beginning the finger is touched on the blue part and when it "moves out" to the white part, it is still continuing to obtain the x, y coordinates. Ideally I want the device to stop detecting the motion event as the finger leaves the screen. Hope you can help.


The easiest solution is to check if your finger is touching the View by simply comparing its x,y coordinates with the Views bounding rect. You can stop receiving events by returning false from dispatchTouchEvent then.


public boolean dispatchTouchEvent(MotionEvent event) {
    Rect outRect = new Rect();
    if (outRect.contains((int) event.getX(), (int) event.getY())) {
        return true; // I want more events
    } else {
        return false; // stop events until next ACTION_DOWN on me