How can I get an "top" View
of my application (which contains Activity
and all DialogFragment
s)? I need to intercept all touch events to handle motion of some View
between DialogFragment
and my Activity
.
I've tried to catch them (event) through the decor view of Activity's Window
with no luck:
getWindow().getDecorView().setOnTouchListener(...);
You can override the Activity.dispatchTouchEvent
to intercept all touch events in your activity, even if you have some Views like ScrollView, Button, etc that will consume the touch event.
Combining withViewGroup.requestDisallowInterceptTouchEvent
, you can disable the touch event of the ViewGroup. For example, if you want to disable all the touch event in some ViewGroup, try this:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
requestDisallowInterceptTouchEvent(
(ViewGroup) findViewById(R.id.topLevelRelativeLayout),
true
);
return super.dispatchTouchEvent(event);
}
private void requestDisallowInterceptTouchEvent(ViewGroup v, boolean disallowIntercept) {
v.requestDisallowInterceptTouchEvent(disallowIntercept);
int childCount = v.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = v.getChildAt(i);
if (child instanceof ViewGroup) {
requestDisallowInterceptTouchEvent((ViewGroup) child, disallowIntercept);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With