ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Android Touch System — Part 2: Common Touch Event Scenarios

Sherry Yuan
ProAndroidDev
Published in
3 min readJan 18, 2022

This is Part 2 of my Android Touch System series. Part 1 does a deep dive into touch functions (onDispatchTouchEvent(), onInterceptTouchEvent(), onTouchEvent()) and how they affect the way touch events flow through the views hierarchy. This post will show visual examples of how these functions work.

The visualizations are based on a sample app I made with subclasses of Activity, FrameLayout, ScrollView, and View that print a log message whenever the functions are called. You can find it on Github and play around with your own implementations of the touch functions.

Here’s a quick recap of the functions covered in Part 1:

Scenario 1

Tap on View B. None of the views consume it

Let’s revisit the visualization from Part 1. Note that View B is added to after View A, so its dispatchTouchEvent() gets called first; ViewGroups pass touch events to their children in reverse order in which they were added.

Scenario 2

Tap on View B. View B’s onTouchEvent() returns true

Since none of View B’s ancestors intercept taps, the event is passed all the way to its onTouchEvent(). Since it returns true, the event stops there and none of the other views’ onTouchEvent()s gets called.

Scenario 3

Tap on View A. None of the views consume it

This is similar to Scenario 1, except since the touch isn’t inside View B’s bounds, FrameLayout passes it to View A directly.

Scenario 4

Scroll on View B. ScrollView intercepts it and shows scrolling

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Sherry Yuan

Android engineer @ Cash App (she/her) • Find my sci-fi/fantasy short stories at sherryyuan.substack.com

No responses yet

Write a response