214 CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus
The Menu Class and Interface: Android Menus
An Android Menu interface was specifically created by the Android OS developers for creating
options menu and context menu structures in Android. Android’s Menu superclass has only two
subclasses: a ContextMenu class and a SubMenu class. These subclasses are used to provide
localized (context-sensitive) menu structures and sub-menu structures for both options menus and
context menus.
The options menu is more of a “global” menu structure, as it is accessed using a hardware
MENU key on your Android device, or, if the Android device does not feature a MENU key, using
the ActionBar “overflow” menu icon, which is those three vertical dots at the right-side of the
ActionBar, at the top of your Android device.
The Menu interface is used for creating Menu object structures, which hold your menu design, much
like a ViewGroup holds your UI screen design. Just like a ViewGroup contains View sub-objects, the
Menu object contains MenuItem objects, along with their configuration parameters, which you will
define using XML mark-up.
As you have already seen, these parameters can include the MenuItem label or “title,” as well as the
order, ID, and various display characteristics, such as an icon, or whether the MenuItem is active (not
dimmed or inactive) or is even visible, or whether it is checkable (can show a check mark next to it
when it has been selected).
The three different types of Menu objects, options menus, context menus, and sub menus all have
different characteristics. For instance, sub menus cannot have nested sub menus and cannot have
MenuItem icons, and context menus also do not support MenuItem icons or shortcuts. In case you
don’t know what “shortcuts” are regarding menus, they are letter (android:alphabeticShortcut) or
number (android:numericShortcut) definitions which the user can use as a shortcut for accessing
that MenuItem more directly or more rapidly.
Options menus, which are the primary type of Menu object used in Android applications, and the
kind we are going to focus on in this chapter, have their own set of rules, such as icon menus
displayed within the ActionBar do not support MenuItem check marks, and will only show the
MenuItem condensed title, if provided using the android:titleCondensed parameter.
You can access the expanded options menu, which will only become available if six (or more)
MenuItems are defined, via the ‘More’ item in the icon menu or ActionBar overflow menu. This
expanded options menu will not show MenuItem icons, and MenuItem check marks are not
recommended for use either.
The Menu object defined by the Menu class and Menu interface will contain MenuItem objects
defined by the MenuItem class, and as you will soon see when we create a working Menu and
MenuItem in the Hello Universe application, both of these classes will have to be imported into your
MainActivity class. There is another menu-related class, the MenuInflater class, which is utilized to
turn or “inflate” your XML menu definition file into a Menu object in much the same way that your UI
design is inflated.