Chapter 31 Custom Views and Touch Events
Run DragAndDraw to confirm that all the connections are correct. All you will see is an empty view
(Figure 31.3).
Figure 31.3 BoxDrawingView with no boxes
The next step is to get BoxDrawingView listening for touch events and using the information from them
to draw boxes on the screen.
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)