Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Like most GUI frameworks, Android uses an event-based model to handle user interaction. When the user taps the screen, a touch event is fired and the corresponding onTouch method of the tapped view is called. By extending the views in your UI, you can receive these events and use them to build gestures into your app; similar methods exist for handling focus and key change events.
These event callbacks form the basis of Android event handling. However, extending every view in your UI is not practical. Further, the low-level nature of the events requires work to implement simple interactions. For this reason, Android has a number of convenience methods for registering listeners on the existing View class. These listeners provide callback interfaces that will be called when common interactions, like tapping the screen, are triggered. To handle these common events, you register an event listener on a view. The listener will be called when the event occurs on that view. You generally want to register the listener on the specific view the user will interact with. For example, if you have a LinearLayout container with three buttons inside, you would register the listeners on the buttons rather than the container object. You have already seen an example of this with the onClick method in the TimeTracker app.