Touch Event Management

dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent

Vladislav Shesternin
5 min readDec 27, 2020
Photo by Yoann Boyer on Unsplash

In this article, you will understand the basic concept of spreading touch events and how to intercept them.

What can you implement by reading this article:

App: Intercept Event

__________________ THEORY __________________

Touch event propagation concept:

Now I’m changing your idea of touch events as happened to me.

Touch

When a click on View occurs, then first of all this event is processed not by View but Activity and then this event, through the rest of the subsidiaries ViewGroup, gradually reaches the same View.

How it works:

Touch sequence

Handling gesture methods:

  • dispatchTouchEvent — dispatch touch events to affected child views;
  • onInterceptTouchEvent — Intercept touch events before passing to children;
  • onTouchEvent — Handle the touch event and stop propogation;
Handling gesture methods

Sequence of method calls:

Note: Nobody processes anything.

Sequence of method calls

onInterceptTouchEvent:

He is only in the ViewGroup and his role is very important, he is needed to intercept events (everything seems to be obvious from the name).

The main thing to know about it is that when it returns:

  • TRUE — this event will be intercepted, that is, it will be processed by the ViewGroup that overrides the given onInterceptTouchEvent.
  • FALSE this event will go further (to a child element that can handle this event).

requestDisallowTouchIntercept:

This is a special method that only ViewGroup has, it is necessary when we do not want our ViewGroup to intercept events.

Example: You have a vertical ScrollView with child vertical ScrollView (or any component (ViewGroup) that needs to use the vertical scroll (MOVE) event).

Request: What do you think will happen with vertical scrolling through the child component???

Answer: The parent ScrollView will be scrolled!!!

SOLUTION: To fix this and force the child ScrollView to respond to scrolling, you must use requestDisallowTouchIntercept(true). To prevent the parent from intercepting the events of the child.

__________________ PRACTICE _________________

Step 1: Create an empty project

Clone the finished project with GitHub.

Empty Project

Step 2: Use viewBinding

gradle (Module)
viewBinding

Step 3: Create class MyConstraintLayout

path
MyConstraintLayout

To override: dispatcherTouchEvent, onInterceptTouchEvent — View/ViewGroup it is necessary to create such classes.

Step 4: Implement activity_main

activity_main

Step 5: The audience’s narrative about parent and child

MainActivity

Now everything works like this: click on Root displays Root, click on Child displays Child.

But what if we want all events to be listened to by the root???

Step 6: onInterceptTouchEvent | All events are intercepted by the root

onInterceptTouchEvent

As we remember, if the onInterceptTouchEvent returns FALSE, then the event goes to the child who will process this event, if TRUE then processes the ViewGroup itself on which the onInterceptTouchEvent is redefined, since we return TRUE to all events, thereby intercepting all events and the child does not get anything.

Dessert:

— — — — — — -GIT

Step 1: Create Empty Project with viewBinding as in the previous example.

Step 2: Create class MyConstraintLayout

MyMotionLayout

Step 3: Create layout activity_main

activity_main

Step 4: Create @xml/scene

scene

Step 5: Create class RecyclerAdapter

RecyclerAdapter

Step 6: Create layout item

item

Step 7: Create class MainActivity

MainActivity

Step 8: Complete the class MyMotionLayout

MyMotionLayout

13 lines, only 13 lines solve this problem. (16 to 29)

Explanation:

Swipe logic RIGHT | LEFT

Conclusion:

dispatchTouchEvent — Override it in Activity if you want to catch the touch event before any of the views have a chance to hit it. For ViewGroup (including the root view), just override onInterceptTouchEvent () and onTouchEvent ().

onInterceptTouchEventOverride if you just want to watch for incoming touch notifications, you can do it here and return false.
However, the main purpose of overriding this method is to allow the ViewGroup to handle a specific type of touch event and a different type for the child.

requestDisallowTouchIntercept — call it if the child does not want its parent to steal the touch event.

Make your application beautiful.

Love what you do and EXPERIMENT !!!!))).

If you have any questions, you can ask them personally to Me at Twitter

--

--

Vladislav Shesternin
Vladislav Shesternin

Written by Vladislav Shesternin

LibGDX | Android Developer | Enthusiast.

No responses yet