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
- ACTION_UP : when finger leaves the screen
- ACTION_MOVE : when moving finger on the screen
A gesture is defined as beginning with ACTION_DOWN and ending with ACTION_UP.
- Events start at the Activity with
- Events flow from top to down through views
- Parents (ViewGroups) dispatch events to their children
- Can intercept events at any time.
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
- 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.
onTouchEvent()returns true, then it means the touch is handled but if it returns false then it means the touch is not handled.
For complex touch interaction:
We have GestureDetector, which is handled via
onTouchListener() or onTouchEvent():
onDown(), onSingleTapUp(), onDoubleTap()
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.