Intercept Touches In Android

In this article, I’ve tried to explain about TouchEvent and describe what happens when the user touches the screen. Always, some questions occur to us while we are trying to implement custom views, “Which one of them I should use? onTouchEvent or onInterceptTouchEvent? What's the difference between them?” 🤔

To gain the answer, come with me 🙃.

The Cycle Of Touch:

The cycle of touch in android os

What would happen when the user touches the screen?

According to below (Next Paragraph) scenario, there are three methods that will be called by touching the screen. dispatchTouchEvent(), onInterceptTouch() and onTouchEvent(). To handle the touch inside of the Activity we should override the dispatchTouchEvent() function, also ViewGroups are the only objects that have the onInterceptTouch() function, thus Views have the onTouchEvent() function. All of them return a boolean to say that they handled the touch or not, and also they get a MotionEvent object with some touch properties in it. When these functions return a “true”, means that these functions handled the touch, if not they didn’t handle the touch.

A Touch Scenario:

By touching the screen by the user, first of all, dispatchTouchEvent() inside the Activity will be called and the event will be passed to the Window. the Window is an abstract class implemented by PhoneWindow. Afterward, the dispatchTouchEvent() of the DecorView will be called. DecorView is a class to handle StatusBar, NavigationBar, etc. As DecorView is inherited by FrameLayout, it has onInterceptTouchEvent(), as well. In fact that we don’t have any access to these two classes, so we ignore them. These classes have been described here, just because of knowing the complete sccenario.

Now, it’s ViewGroups’ turn. Here we have two ViewGroups, A and B. As it is obvious in the diagram (Figure NO. 2), if onInterceptTouchEvent() of ViewGroupA and ViewGroupB return “true” they will call ViewGroup’s onTouchEvent(), if not, the Event will be passed to the View. If OnTouchListener of the View is not null, means it is initialized by setOnTouchListener(), so, after dispatchTouchEvent() and onTouchListener() inside the View, OnTouchListener.onTouch() and onTouchEvent() will be called by it. If OnTouchListener is null, onTouchEvent() can just be called.

Oops! For better understanding, flow the path in the diagram. You can simplify it by separating it into two parts, flows of onInterceptTouchevent and onTouchEvent. It is clear that with touching the screen by a user, first of all, onInterceptTouchEvent() will be called until the last View if all of them return “false”. Now, onTouchEvent will be called until the Activity.

Disallow Touch Intercept

Up to now, I’ve tried to explain the cycle of Touches. Now, supposing that a custom view has been implemented by you, for instance, a progress bar, and you want to use it inside scrollable views such as ScrollView. While you are trying to change the view’s progress, the ScrollView will be scrolled!

So, What can you do here? You can call requestDisallowTouchIntercept() within onTouchEvent() of the custom view. It will stop higher views from intercepting touch events.

If you see any problem here, please guide me to fix them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store