ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Android Touch System — Part 1: Touch Functions and the View Hierarchy

Sherry Yuan
ProAndroidDev
Published in
6 min readJan 18, 2022

Photo by Killian Cartignies on Unsplash

Without a deep understanding of how Android views handles touches, a lot of touch behaviours seem confusing. Why is this button click not working? Why is that RecyclerView not scrolling? How do I handle nested ScrollViews?

This article covers how touch events flow through the view hierarchy, and how a few core functions affect the flow. Part 2: Common Touch Event Scenarios shows concrete, visual examples of how these functions.

Table of Contents

Background Knowledge

MotionEvent

Every movement on the touch screen is reported as a MotionEvent object. The MotionEvent class has getters for accessing all the information associated with the event. Some commonly-used ones are:

  • action (the type of action being performed, more on this later)
  • x (x-coordinate of touch, relative to the view that handled it)
  • y (y-coordinate of touch, relative to the view that handled it)
  • rawX (absolute x-coordinate of touch, relative to the device screen)
  • rawY (absolute y-coordinate of touch, relative to the device screen)
  • eventTime (the time the event occurred, in the SystemClock.uptimeMillis() time base)

Screen coordinates

As a reminder, Android screen coordinates are measured in pixels with x = 0 and y = 0 in the top left corner of the screen and the x = maxX and y = maxY in the bottom right corner.

Action

MotionEvent’s getAction() returns an Int constant representing different action types. The full list can be found here, but here are some of the most common ones:

  • ACTION_DOWN: When finger or object first comes in contact with the screen. The event contains the initial starting location of a gesture.
  • ACTION_UP: When finger or object lifts from the…

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

Responses (1)

Write a response