29 Custom Views and Touch Events

In this chapter, you will learn how to handle touch events by writing a custom subclass of View named BoxDrawingView. The BoxDrawingView class will be the star of a new project named DragAndDraw and will draw boxes in response to the user touching the screen and dragging. The finished product will look like Figure 29.1.

Figure 29.1  Boxes drawn in many shapes and sizes

Boxes drawn in many shapes and sizes

Setting Up the DragAndDraw Project

Create a new project named DragAndDraw. Select API 16 as the minimum SDK and create a blank activity. Name the activity DragAndDrawActivity.

Setting up DragAndDrawActivity

Get Android Programming: The Big Nerd Ranch Guide, 2nd 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.