Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Touch Control and Events in Android

  • Last Updated : 20 Dec, 2021

Almost all Android phones today are touch-controlled. There are very few phones that do not have a touchscreen. In this article, we will discuss how to handle touch events in Android. Understanding how to work around touch controls and touch events when we tap on our screen is an example of this. We’ll go over how Android’s touch events work internally for any view. So, how exactly do input events work, and what happens when we touch our screen when we have a ViewGroup with multiple views inside it? In this case, we’ll look at a LinearLayout with a button on it, such as:

Image #1: The linear layout and the button.

What we will be discussing in this geeks for geeks article is,

  1. What happens when we press our fingers against the screen?
  2. How do we stop touch events?
  3. How is touch handled?
  4. What are the main touch events that we use in Android to handle touch control?
  5. What happens when we press our fingers against the screen?

So, when we touch the screen, the activity’s view, also known as DecorView in Android, receives the touch event notification first. Now, we don’t usually work with the DecorView touch. As a result, the touch is transferred to the ViewGroup and then to its children in the XML file.

But how do we move the touch event trigger?

Using dispatchTouchEvent, the ViewGroup in Android transfers the touch event from top to bottom in the ViewGroup hierarchy to its children ().

Image #2: The dispatch event module

How do we stop touch events?

To begin, when we perform a touch action, The touch event is then received by the ViewGroup, and it is intercepted within the ViewGroup using onInterceptTouchEvent ().

Geek Tip: If we return true after intercepting, the touch event is not passed to its children; if we return false, the Android eco-system is notified that the ViewGroup wants to dispatch the event to its children, which in our case is a button.

In general, if we return true, it means we handled the event within the ViewGroup itself, and there is no need to dispatch to its children. The view we have now is the last view in our view tree, as indicated by the button. As a result, it will no longer be able to pass on the touch event to its children because it no longer has any. So, in button, our last onInterceptTouchEvent would be called.

In this view, how is touch handled?

We need to check the hierarchy, of all the views that we take into account and then perform our work. Handling the event is all about the priority assigned to everything, the first, and then it moves towards the ViewGroups. Touch events function similarly to event dispatching, but in reverse order from child to parent. Let’s say we dispatch the event from ViewGroup and intercept it there; the return value (true/false) determines whether the view’s touch is handled on the ViewGroup or the children.

Image #3: The dispatchTouchEvent and onTouch

So, in this article, the feedback received from the button returns all true, it means that it has been handled and will not be sent to the LinearLayout.

What are the main touch events that we use in Android to handle touch control?

When we receive a touch event, it is handled by onTouchEvent, which also has a MotionEvent parameter.


fun onTouchEvent(event: MotionEvent)

The event parameter contains a reference to every task performed in relation to the touch. On the screen of the point on the touch, we can have coordinates such as X and Y points. It even has actions in it, for example, if we tap on the screen, MotionEvent.ACTION DOWN is called, and if we lift our finger, MotionEvent.ACTION UP is called. The action is MotionEvent even if you drag your finger across the screen using MorionEvent.ACTION MOVE. So, when we want to tap the button, the flow on the view occurs.

onTouchEvent -> Activity -> dispatchTouchEvent (LinearLayout) -> dispatchTouchEvent (Button) -> onTouchEvent (Button).

When we don’t want to tap the button but still want to handle the click on LinearLayout, the flow is as follows:

Activity -> dispatchTouchEvent (LinearLayout) -> dispatchTouchEvent(Button) -> onTouchEvent(Button) (will return false) -> onTouchEvent(Button) -> onTouchEvent(Button) (will return false) -> onTouchEvent(Button) (LinearLayout).


  1. When working with touch events, we begin by clicking a view and then removing the gesture (in our case, our finger/stylus), after which MotionEvent.ACTION DOWN and MotionEvent.ACTION UP is called.
  2. When the initial touch occurs on the ViewGroup and the child is intercepted.
  3. MotionEvent.ACTION CANCEL is called on the ViewGroup and the touch event is dispatched to the children.
  4. Everything now depends on the return value of onInterceptTouchEvent().
  5. The dispatchTouchEvent is dependent on its return value; if it returns true, the dispatcher is canceled; if it returns false, the dispatching of the touch event continues until it is used.
  6. And if onTouchEvent() returns true, it means the touch was handled; if it returns false, it means the touch was not handled
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!