O'Reilly logo

Android Programming: The Big Nerd Ranch Guide by Brian Hardy, Bill Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Touch Events

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

 ​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​s​e​t​O​n​T​o​u​c​h​L​i​s​t​e​n​e​r​(​V​i​e​w​.​O​n​T​o​u​c​h​L​i​s​t​e​n​e​r​ ​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:

 ​ ​ ​ ​p​u​b​l​i​c​ ​b​o​o​l​e​a​n​ ​o​n​T​o​u​c​h​E​v​e​n​t​(​M​o​t​i​o​n​E​v​e​n​t​ ​e​v​e​n​t​)​

This method receives an instance of MotionEvent, a class that describes the touch event, including its location ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required