Android Touch Framework

Adil Khan
Adil Khan
May 11 · 4 min read

Understanding concepts of Touch framework

Android Touch

Hello fellow developers, in this article I am going to discuss about the Android touch framework, how the touches flow through the application and how Android handles all of those events.

How Android deals with Touch

Every touch event into your application is wrapped up as a MotionEvent. These MotionEvent contains description of action and some metadata such as location of touch event on the screen. Some of the actions are as follows :

  • ACTION_DOWN : when finger touches the screen

A gesture is defined as beginning with ACTION_DOWN and ending with ACTION_UP.

  1. Events start at the Activity with dispatchTouchEvent()
  • Parents (ViewGroups) dispatch events to their children

3. Events flow down the hierarchy and then back up until consumed

When handling the dispatching of the event, the top position of the hierarchy takes the lead, but when it comes to handling the touch, the child views using onTouchEvent are always the first, and then it keeps moving towards the ViewGroups.

Touch event works just like the dispatching of the events but in the reverse order from child to parent.

While intercepting if we return true then the touch event is not passed to its children and if we pass false, the Android eco-system gets notified that the ViewGroup wants to dispatch the event to its children,

Child can call requestDisallowTouchIntercept() to block onInterceptTouchEvent() for the duration of current gesture. For example some scrolling view inside a Scrollview. We want the parent scroll to stop while scrolling the child. This flag is reset by framework on new gesture (ACTION_DOWN)

Scenario 1— When no view is consuming the event

Scenario 2 — When a view is consuming the event

Scenario 3 —

In case of a button inside a ScrollView, the button consumes the touch event when clicked but if the gesture is changed to dragging then onInterceptTouchEvent of the ScrollView gets called and ScrollView starts handling the touch and the button receives an ACTION_CANCEL event.

  • Everything depends on onInterceptTouchEvent() and its return value. The dispatchTouchEvent is dependent on the value of onInterceptTouchEvent(), if it returns true the dispatch is canceled, and if it returns false then the dispatching of the touch event keeps going on until its used.

For complex touch interaction:

We have GestureDetector, which is handled via onTouchListener() or onTouchEvent():

  • onDown(), onSingleTapUp(), onDoubleTap()

TouchDelegate

Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate.

Allows for the touch area of a specific view to be different than its actual touch bounds.

I hope you learned something! Thank you for reading.

Click the 👏 to show your support and share it with other fellow Medium users.

You can find me on Twitter and Instagram

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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