Questions regarding designing, implementing and handling menus and action bars in Android. A menu is a user accessible list of commands that typically provide additional functionality that may not be core to the generally displayed user interface.
Introduction
Many Android devices (prior to Android 3.0) have a hardware menu button. When the menu button is pressed, a list of additional user commands are made visible for the user to select from. Menu Items allows for both text and icons.
Android 3.0+ devices utilize an on screen menu called the action bar. In contrast to the normal menu, users do not press a hardware menu button to display the options. In fact, many devices using 3.0 have removed the hardware button entirely. In Android, the menu and action bar serve the same purpose. When developed correctly, both may be supported using similar Java code and/or combined Android XML.
Using Android XML
Android XML provides the ability to separate menu design from actual implementation. While this may be tweaked in code, or handled completely with Java, the menu XML is a valuable resource.
Android Menu Handling
There are three primary methods for handling the menu, regardless of how it is designed. These will work on an Android 3.0+ device for supporting the action bar's basic functionality.
onCreateOptionsMenu()
- This event is fired when the user presses the Menu button for the first time. If the device is Android 3.0+, this will create the Action bar.onPrepareOptionsMenu()
- This event is fired when the Menu is actually opened. If this is the first time the button is pressed, it will fire immediately afteronCreateOptionsMenu()
onOptionsItemSelected()
- This event is fired whenever a user chooses a MenuItem from the menu. This event fires in Android 3.0+.onCreateContextMenu()
onContextItemSelected()
Action Bar Handling
The native Action Bar utilizes the same methods as the Menu. For extra functionality, the Action Bar also adds the following interfaces to be implemented:
- TabListener - enables Tabs in the ActionBar
- OnMenuVisibilityListener - allows objects to respond to Menu visibility
- OnNavigationListener - allows set up of navigation modes using Action Bar