I would like to process all touch events at the container level. In other words, I want all touch events to reach the container level. Right now I am finding that TouchEvents only reach the containing ViewGroup if the location of the touch does not land on a UI Component that is contained by the view group. The contained UI Element processes the TouchEvent and it does not bubble up to the container. Does anyone know how to guarantee that all touch events reach the top level container?
Just to be clear, picture an activity with several buttons, several edit texts, and several check boxes. A typical form. Normally I am seeing that each UI component will catch the TouchEvent that lands on it and the container is none the wiser. So I want to know how the container/viewgroup could be informed of all touches that land anywhere within its region whether or not that region is occupied with a button, empty space or an edit text.
This is how I interpret your question:
You have View tree like this
and you want G to handle the MotionEvent created on touch even if it was consumed by any of A, B, ...
How to:
I'll give you two options. Depending on if you want to ignore consumption by
use one of the following templates:
Override dispatchTouchEvent() in G
// In G
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(event);
// Handle event here instead of using an OnTouchListener
return true;
}
Override dispatchTouchEvent() in every child whose consumption should be ignored
Ignore some children
// In every child whose consumption should be ignored
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(ev);
return false; // false means "I did not consume the event"
}
Don't forget to setup your listener
// In G, before touch should be recognized
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Handle event here
return true; // true means "I did consume the event"
}
});
Measured in lines of code, option 1 is a lot simpler. Option 2 is there just in case you need special treatment for some of A, B ...
I know it is late, but I think future readers might want to look at ViewGroups.onInterceptTouchEvent().
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