Handling Touch Events

One way to listen for touch events is to set a touch event listener using the following View method:

    public void setOnTouchListener(View.OnTouchListener l)

This method works the same way as setOnClickListener(View.OnClickListener). You provide an implementation of View.OnTouchListener, and your listener will be called every time a touch event happens.

However, because you are subclassing View, you can take a shortcut and override this View method instead:

    public boolean onTouchEvent(MotionEvent event)

This method receives an instance of MotionEvent, a class that describes the touch event, including its location and its action. The action describes the stage of the event:

Action constants ...

Get Android Programming: The Big Nerd Ranch Guide, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.