Understanding Android Menus

Whether you've worked with Swing in Java, with WPF (Windows Presentation Foundation) in Windows, or with any other UI framework, you've no doubt worked with menus. In addition to providing comprehensive support for menus, Android presents some new patterns such as XML menus and alternative menus.

We will start this chapter by describing the basic classes involved in the Android menu framework. In the process, you will learn how to create menus and menu items, and how to respond to menu items. The key class in Android menu support is android.view.Menu. Every activity in Android is associated with a menu object of this type, which can contain a number of menu items and submenus. Menu items are represented by android.view.MenuItem and submenus are represented by android.view.SubMenu. These relationships are graphically represented in Figure 5-1. Strictly speaking, this is not a class diagram, but a structural diagram designed to help you visualize the relationships between the various menu-related classes and functions.

Contains a single menu onCreateOptionsMenu (callback)

onOptionsItemSelected

(callback)

Figure 5-1. Structure of Android menu classes

Contains a single menu onCreateOptionsMenu (callback)

onOptionsItemSelected

(callback)

Menu Module

Menu

Contains 0 or more

MenuItem

SubMenu

Figure 5-1. Structure of Android menu classes

You can group menu items together by assigning each one a group ID, which is merely an attribute. Multiple menu items that carry the same group ID are considered part of the same group. In addition to carrying a group ID, a menu item also carries a name (title), a menu-item ID, and a sort-order ID (or number). You use the sort-order IDs to specify the order of menu items within a menu. For example, if one menu item carries a sort-order number of 4 and another menu item carries a sort-order number of 6, then the first menu item will appear above the second menu item in the menu.

Some of these order-number ranges are reserved for certain kinds of menus. Secondary menu items, which are considered less important than others, start at 0x30000 and are defined by the constant Menu.CATEGORY_SECONDARY. Other types of menu categories—such as system menus, alternative menus, and container menus—have different order-number ranges. System menu items start at 0x20000 and are defined by the constant Menu. CATEGORY_SYSTEM. Alternative menu items start at 0x40000 and are defined by the constant Menu.CATEGORY_ALTERNATIVE. Container menu items start at 0x10000 and are defined by the constant Menu.CATEGORY_CONTAINER. By looking at the values for these constants, you can see the order in which they'll appear in the menu. (We'll discuss these various types of menu items in the "Working with Other Menu Types" section.)

Figure 5-1 also shows two callback methods that you can use to create and respond to menu items: onCreateOptionsMenu and onOptionsItemSelected. We will cover these in the next few subsections.

0 0

Post a comment