I respond to touch events inside a custom View in onTouchEvent(MotionEvent event)
. I'm having trouble with an inconsistency in coordinates: event.getRaw(Y)
returns the Y coordinate of a touch including the status bar, but myView.getTop()
returns the Y coordinate of the top of the view excluding the status bar. I've resorted to the following hack to correct for the height of the status bar:
// Get the size of the visible window (which excludes the status bar)
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the coordinates of the touch event, subtracting the top margin
final int x = (int) event.getRawX();
final int y = (int) event.getRawY() - rect.top;
Is there a better solution?