213
Chapter 7
Making Apps Interactive: Intents,
Event Handling, and Menus
User interface design is built upon the foundation of the Activity superclass as well as the View and
ViewGroup superclasses, which you learned all about in the previous chapter. However, without any
interactivity in your UI design, the usefulness of what you can do with your Android application is
somewhat limited!
For this reason, I’m going to cover several fairly advanced concepts fairly early on in this book, such
as how the Menu and MenuItem classes work in Android, as well as how Intent objects can be
utilized to switch over to your Activity subclasses. Using Intent objects, you can switch between
functional screens in your applications, each of which is defined using an Activity subclass that
references a UI layout container (ViewGroup) subclass.
Besides covering Android’s menu-related classes, we will also cover the broader concepts of Intents
and Intent Filters, as they relate to Android. You first encountered Intent Filters in Chapter 4
(refer to Figure 4-17 near the bottom of the screen shot) when we looked over your bootstrap
AndroidManifest.xml file in detail.
In this chapter, we’ll implement an “Edit Galaxy Info” menu item in the HelloUniverse app, and
make the menu item call an EditGalaxy class, using an Intent object. We’ll learn more about the
RelativeLayout layout class, developing a new UI design. This will allow users to update Galaxy
objects via Button and EditText widgets.
Adding interactivity to your user interface elements, or widgets, will require that you learn what Event
Handlers are, and how to implement Event Listeners, which are used to make the UI elements (widgets)
interactive. Once the RelativeLayout UI design is completed and referenced using the onCreate( )
and setContentView( ) methods, we can instantiate your Button and EditText objects, and then attach
onClick( ) event handler Java structures to them, so that your user can use them interactively to update
the statistics for the Galaxy object. Since we are covering Menu, MenuItem, Intent, RelativeLayout,
Button, EditText, and onClick( ) event-handling routines all in a single chapter, we had better get started
right away. Hang on tight, you’re in for an exciting journey through user interface land!