Android Touch Input

Notes from AnDevCon IV – Touch Input

Start with dispatchTouchEvent() flow down until consumed by a view
End back to Activity with onTouchEvent() <– only if not consumed, last in chain
OnTouchListener optional always called first
onInterceptTouchEvent() can grab touch events from children, passes ACTION_CANCEL to child
View group send touch events to children in reverse order of added order to match z order

After first event is consumed additional events are routed directly to the consumer
Must return true on all events if your view is interested in any events else all events will be routed directly to the activity

return super() when not returning true


Class contains constants for view identification
getScaledTouchSlop() < dragged movement

Touch Event Forwarding

Call targets dispatchTouchEvent() not onTouchEvent()
onInterceptTouchEvent() is a one time shot
View.onTouchEvent() does a lot of state management, call super() when in doubt
protect ACTION_MOVE with slop checks


touch down and up event contain pointer index
MotionEvent.getActionMasked() to auto mask events


ACTION_MOVE events can be batched
Latest is returned by standard methods – getX() getY()
getHistoricalXXX(int index) methods to get rest of events

ScaleGestureDetector looks for pinch (api 2.2+)


TouchDelegate job is to forward from parent to child *
For very small target
Can make a views touchable area large within bounds of parent view
ViewGroup.setTouchDelegate( new TouchDelegate(Rect bounds, View child);
When event is send to child view X Y is centered on child view

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *