ultra
ultra

Reputation: 329

How do I differentiate between MotionEvent.ACTION_UP and dragging a finger off-screen in Android?

My application is a grid of buttons, edge to edge. I'm overriding all touch and motion events and controlling the behavior manually. Everything is working fine, except for when a finger is dragged off-screen. MotionEvent.ACTION_UP is triggered, and I don't want that. I tried using getRawX() and getRawY() to determine if (for example, for the top and left edges) the coordinate is < 1, but the value will be higher (as high as 20) if a finger is dragged quickly. When ACTION_UP is triggered, the location values seem to lag behind a bit. Is there a way around this? ACTION_CANCEL doesn't seem to get triggered at all.

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case (MotionEvent.ACTION_DOWN):
        Log.i(classID, "action down");
        return true;
    case (MotionEvent.ACTION_CANCEL):
        Log.i(classID, "action cancel");
        // never called
        return true;
    case (MotionEvent.ACTION_MOVE):
        Log.i(classID, "action move");
        return true;
    case (MotionEvent.ACTION_UP):
        Log.i(classID, "action up");
        Log.i(classID, "position: " + (int)event.getRawX() + "/" + (int)event.getRawY());
        return true;
    }
    return false;
}

Upvotes: 2

Views: 3732

Answers (2)

pepe le vamp
pepe le vamp

Reputation: 1

What he said about there being 'no physical difference' between the finger being lifted off the screen and the finger being swiped out of the borders of your View is incorrect.

You can prove so by merely having a SurfaceView in the middle of your screen, and in the OnTouch event callback, have an IF statement such as:

if (event.getEvent() == MotionEvent.ACTION_UP) {
         Log.d("derp", "finger has lifted up");
}

You'll see that it doesn't actually report if your finger merely goes out of the boundaries. Although there is a MotionEvent.ACTION_OUTSIDE, I can't seem to get it working. Hence why I'm looking about here too.

Upvotes: 0

ahodder
ahodder

Reputation: 11439

There is physical or logical no difference between the two (finger falling off the screen and lifting your finger). When your finger goes off the screen, you have "lifted ", or, better worded, removed your finger from the screen. MotionEvent.ACTION_UP is a place holder for any motion event that results in no touch input data being provided to the system.

If you wish to work around this, you could possibly create a parent view and child view. If the touch is "lifted" from the child view but still in the parent, the finger is still on the device but out of bounds.

Upvotes: 2

Related Questions