252 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus
Mouse-over and import each class, then instantiate the objects using an instantiateUi( ) method,
seen in Figure 7-35.
Figure 7-35. Create a private void instantiateUi( ) method, instantiate UI elements, and call method from onCreate( )
Now that you have set up your UI elements as objects in your Java code, as well as practiced
making your own instantiateUi( ) method to better organize your Java code statements, it is time to
create an OnClickListener( ) event handling construct. I wanted to cover this as early on in this book
as I could, as we will be using this type of event handling code structure throughout the book, so
please pardon this lengthy chapter! We’re almost done!
Creating an Event Listener: .onClickListener( )
The .OnClickListener( ) constructor method (notice in Figure 7-36 that the method starts with a
capital letter) is called off of the View class using the Java new keyword (since we are constructing
a new View class OnClickListener object), inside of the .setOnClickListener( ) method called off of
your UI widget. In this case, it’s the colonyButton object.