Handling Intercept touch event without extending ViewGroup

RuntimeException picture RuntimeException · Jan 9, 2014 · Viewed 17.6k times · Source

I have a one RelativeLayout and this layout is having nearly 10 views in it.

I have set OnTouchListener to this Layout and doing some work in it and returning true.

this listener is working fine when I touch the layout where there are no View (mean on Empty area). If I touch on child views of this layout, this listener is not firing...

and from the documentation, I understood that we can override onInterceptTouchEvent() by extending ViewGroup (so here RelativeLayout) and handle touch events before child views consume this event...

this will do a trick, but I need to modify many xml files where I need this functionality by replacing RelativeLayout with my CustomRelativeLayout.

so my question is:

is there any way to handle touch event for RelativeLayout (ofcourse ViewGroup) before child views in RelativeLayout consumes event? I don't want to extend RelativeLayout...

Answer

Michael Katkov picture Michael Katkov · Jan 9, 2014

Try to override

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    return super.dispatchTouchEvent(ev);
}

of Activity. This method is the first that process touch events. But in this case you need to work with current coordinates of views