6.8.13. Overriding View Method onTouchEvent

In this example, we override View method onTouchEvent (Fig. 6.19) to determine when the user touches the screen. The MotionEvent parameter contains information about the event that occurred. Line 523 uses the MotionEvent’s getAction method to determine which type of touch event occurred. Then, lines 526–527 determine whether the user touched the screen (MotionEvent.ACTION_DOWN) or dragged a finger across the screen (MotionEvent.ACTION_MOVE). In either case, line 529 calls the cannonView’s fireCannonball method to aim and fire the cannon toward that touch point. Line 532 then returns true to indicate that the touch event was handled.

518      // called when the user touches ...

Get Android™ How to Program, Second 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.