-11).

Figure 10-11.
The auto-flipping ViewFlipper
is useful for status panels or other situations where you have a lot of information to display, but not much room. The key is that, since it automatically flips between views, expecting users to interact with individual views is dicey — the view might switch away part-way through their interaction.
Other Containers of Note
Android offers AbsoluteLayout
, where the contents are laid out based on specific coordinate positions. You tell AbsoluteLayout
where to place a child in precise X,Y coordinates, and Android puts it there, no questions asked. On the plus side, this gives you precise positioning. On the minus side, it means your views will only look “right” on screens of a certain dimension, or it requires you to write a bunch of code to adjust the coordinates based on screen size. Since Android screens might run the gamut of sizes, plus have new sizes crop up periodically, using AbsoluteLayout
could get quite annoying.
Android also has a new flavor of list, the ExpandableListView
. This provides a simplified tree representation, supporting two levels of depth: groups and children. Groups contain children; children are “leaves” of the tree. This requires a new set of adapters, since the ListAdapter
family does not provide any sort of group information for the items in the list.
CHAPTER 11
Applying Menus
Like applications for the desktop and some mobile operating systems, such as Palm OS and Windows Mobile, Android supports activities with application menus. Some Android phones will have a dedicated menu key for popping up the menu; others will offer alternate means for triggering the menu to appear.
Also, as with many GUI toolkits, you can create context menus. On a traditional GUI, this might be triggered by the right mouse button. On mobile devices, context menus typically appear when the user taps-and-holds over a particular widget. For example, if a TextView
had a context menu and the device was designed for finger-based touch input, you could push the TextView
with your finger, hold it for a second or two, and a pop-up menu would appear for you to choose from.
Android differs from most other GUI toolkits in terms of menu construction. While you can add items to the menu, you do not have full control over the menu’s contents, nor of the timing of when the menu is built. Part of the menu is system-defined, and that portion is managed by the Android framework itself.
Flavors of Menu
Android considers application menus and context menus to be the options menu and the context menu, respectively. The options menu is triggered by pressing the hardware Menu button on the device, while the context menu is raised by a tap-and-hold on the widget sporting the menu.
In addition, the options menu operates in one of two modes: icon and expanded. When the user first presses the Menu button, the icon mode will appear, showing up to the first six menu choices as large, finger- friendly buttons in a grid at the bottom of the screen. If the menu has more than six choices, the sixth button will become. More — clicking that option will bring up the expanded mode, showing the remaining choices not visible in the regular menu. The menu is scrollable, so the user can get to any of the menu choices.
Menus of Options
Rather than building your activity’s options menu during onCreate()
, the way you wire up the rest of your UI, you instead need to implement onCreateOptionsMenu()
. This callback receives an instance of Menu
.
The first thing you should do is chain upward to the superclass (super.onCreateOptionsMenu (menu)
) so the Android framework can add in any menu choices it feels are necessary. Then you can go about adding your own options, described momentarily.
If you will need to adjust the menu during your activity’s use (e.g., disable a now-invalid menu choice), just hold onto the Menu instance you receive in onCreateOptionsMenu()
or implement onPrepareOptionsMenu()
, which is called just before displaying the menu each time it is requested.
Given that you have received a Menu object via onCreateOptionsMenu()
, you add menu choices by calling add()
. There are many flavors of this method, which require some combination of the following parameters:
• A group identifier (int
), which should be NONE unless you are creating a specific grouped set of menu choices for use with setGroupCheckable()
(see the following list).
• A choice identifier (also an int) for use in identifying this choice in the onOptionsItemSelected()
callback when a menu choice is selected.
• An order identifier (yet another int), for indicating where this menu choice should be slotted if the menu has Android-supplied choices alongside your own — for now, just use NONE
.
• The text of the menu choice, as a String
or a resource ID.
The add()
family of methods all return an instance of MenuItem
, where you can adjust any of the menu-item settings you have already set (e.g., the text of the menu choice). You can also set the shortcuts for the menu choice — single-character mnemonics that select that menu choice when the menu is visible. Android supports both an alphabetic (or QWERTY) set of shortcuts and a numeric set of shortcuts. These are set individually by calling setAlphabeticShortcut()
and setNumericShortcut()
, respectively. The menu is placed into alphabetic shortcut mode by calling setQwertyMode()
on the menu with a true
parameter.
The choice and group identifiers are keys used to unlock additional menu features, such as these:
• Calling MenuItem#setCheckable()
with a choice identifier to control if the menu choice has a two-state checkbox alongside the title, where the checkbox value gets toggled when the user chooses that menu choice
• Calling Menu#setGroupCheckable()
with a group identifier to turn a set of menu choices into ones with a mutual-exclusion radio button between them, so one out of the group can be in the “checked” state at any time
You can also call addIntentOptions()
to populate the menu with menu choices corresponding to the available activities for an intent (see Chapter 25).
Finally, you can create fly-out sub-menus by calling addSubMenu()
and supplying the same parameters as addMenu()
. Android will eventually call onCreatePanelMenu()
, passing it the choice identifier of your sub-menu, along with another Menu
instance representing