This is what I used to find the first view without looking at the group under my finger. It is recursive and uses getGlobalVisibleRect
private View findViewAtPosition(View parent, int x, int y) {
if (parent instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)parent;
for (int i=0; i<viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
View viewAtPosition = findViewAtPosition(child, x, y);
if (viewAtPosition != null) {
return viewAtPosition;
}
}
return null;
} else {
Rect rect = new Rect();
parent.getGlobalVisibleRect(rect);
if (rect.contains(x, y)) {
return parent;
} else {
return null;
}
}
}
Usage example:
public boolean onTouchEvent(MotionEvent ev) {
Log.i(TAG, "View under finger: " + findViewAtPosition(getRootView(), (int)ev.getRawX(), (int)ev.getRawY()));
}
source
share