UI Menus
- Options menus
- The menu that appears when the user presses the menu button on the action bar.
- Menus are written in XML in a <menu> element
- Each menu item is written in an <item> element
- Tags include
- icon - menu item icon
- title - menu item text
- app:showAsAction - put icons in action bar, always or
ifRoom, etc.
- Items can be enabled/disabled, made visible/invisible, and can have checkboxes
- Items can have a submenu inside them (see below)
- The easiest way of making a menu is inflating an XML file via MenuInflater in
the onCreateOptionsMenu function.
- Handle clicks in onOptionsItemSelected.
Return true for those handled, and call the super class for others.
- Menu icons
- Submenus
- A submenu is a floating list of menu items that appears when the user touches a menu
item that contains a nested menu.
- Context menus
- A floating list of menu items that appears when the user long presses a view that's
registered to provide a context menu.
- A context menu is used to provide access to actions that pertain to a specific item in
the user interface.
- In order for a UI element to provide a context menu, it must be registered using
registerForContextMenu.
- onCreateContextMenu is called each time the element is long-pressed.
- Handle clicks in onContextItemSelected.
Return true for those handled, and call the super class for others.
- Checkable menu items
- You can define the checkable behavior for individual menu items using the
checkable attribute in the item element, or for an entire group with the
checkableBehavior attribute in the group element.
- checkableBehavior can be single, all, or none.
- You have set the state of the radio button or checkbox.
- Example
MainActivity.java
activity_main.xml
main_menu.xml
context_menu.xml
strings.xml
dimens.xml