5.User Interface/Menu
1. Menu
Three fundamental types of menus or action presentation on all versions of Android:
<1>Option menu and action bar
Android 2.3 or lower, reveal the options menu panel by pressing MEnu button
Android 3.0 or higher, options menu are presented by the action bar as a combination of on-screen action items and overflow options
<2>Context menu and contextual action mode
floating menu that appears when user performs a long_click on an element
<3>Popup menu
displays a list of items in a vertical list that's anchored to the view that invoked the menu.
2. Defining a Menu in XML
define a menu and all its items in an XML menu resource. then inflate the menu resource(load it as a Menu object) in activity or fragment
<menu> a container for menu items.
<item> a single item in a menu
This element may contain a nested <menu> element in order to create a submenu.
<group> invisible container for <item> elements
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> // File menu <item android:id="@+id/file" android:title="@string/file" > <!-- "file" submenu --> <menu> <item android:id="@+id/create_new" android:title="@string/create_new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> // Edit Menu <item android:id="@+id/edit" .......> </menu>
3. Creating an options Menu
Android 2.3 or lower. the contents of your options menu appear at the bottom of the screen when the user presses the Menu button,
Android 3.0 and higher, items from the options menu are available in the action bar.
By default, the system places all items in the action overflow, which the user can reveal with the action overflow icon on the right side
of the action bar
To enable quick access to important actions, you can promote a few items to appear in the action bar by adding
android:showAsAction="ifRoom" to the corresponding <item> elements
To specify the options menu for an activity, override onCreateOptionMenu(), inflate menu resource(defined in XML) into the Menu
provided in the callback
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; }
add menu items using add() and retrive items with findItem() to revise their properties with MenuItem APIs
3.1 Handling click events
selects an item from the options menu, system calls activity`s onOptionsItemSelected() method
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: //item`s id newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } }
3.2 Changing menu items at runtime
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the
onPrepareOptionsMenu() method.This method passes the Menu objects as it currently exsits so you can modify it, such as add,
remove, or disable items
On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When
an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call
onPrepareOptionsMenu().
4. Creating Contextual Menus
There are two ways to provide contextual actions:
<1> In a floating context menu (left)
<2> In a contextual action mode (right)
4.1 floating context menu
<i> Register the View to which the context menu should be associated by calling registerForContetxMenu() and pass it the View
<ii> Implement the onCreateContextMenu() method
When the registered view receives a long-click event, the system calls onCreateContextMenu()
This is where you define the menu items, usually by inflating a menu resource
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); }
<iii> Implement onContextItemselected()
@Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.edit: editNote(info.id); return true; case R.id.delete: deleteNote(info.id); return true; default: return super.onContextItemSelected(item); } }
4.2 contextual action mode
4.2.1 Enabling the contextual action mode for individual views
<i> implement the ActionMode.Callback interface
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { // Called when the action mode is created; startActionMode() was called @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate a menu resource providing context menu items MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } // Called each time the action mode is shown. Always called after onCreateActionMode, but // may be called multiple times if the mode is invalidated. @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; // Return false if nothing is done } // Called when the user selects a contextual menu item @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.menu_share: shareCurrentItem(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } // Called when the user exits the action mode @Override public void onDestroyActionMode(ActionMode mode) { mActionMode = null; } };
<ii> call startActionMode() to enable the contextual action mode whem appropriate, such as in response to a long-click on a View
someView.setOnLongClickListener(new View.OnLongClickListener() { // Called when the user long-clicks on someView public boolean onLongClick(View view) { if (mActionMode != null) { return false; } // Start the CAB using the ActionMode.Callback defined above mActionMode = getActivity().startActionMode(mActionModeCallback); view.setSelected(true); return true; } });
4.2.2 Enabling batch contextual actions in a ListView or GridView
ListView listView = getListView(); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { // Here you can do something when items are selected/de-selected, // such as update the title in the CAB } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { // Respond to clicks on the actions in the CAB switch (item.getItemId()) { case R.id.menu_delete: deleteSelectedItems(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { // Here you can make any necessary updates to the activity when // the CAB is removed. By default, selected items are deselected/unchecked. } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { // Here you can perform updates to the CAB due to // an invalidate() request return false; } });
5. Creating a Popup Menu
A PopupMenu is a model anchored to a View
Here is a button with the android:onClick attribute that shows a popup menu
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_overflow_holo_dark" android:contentDescription="@string/descr_overflow_button" android:onClick="showPopup" />
The activity can then show the popup menu like this:
public void showPopup(View v) { PopupMenu popup = new PopupMenu(this, v);
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(this);
MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.actions, popup.getMenu()); popup.show(); }
Handling click events
@Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.archive: archive(item); return true; case R.id.delete: delete(item); return true; default: return false; } }
6. Creating Menu Groups
A meuu group is a collection of menu items that share certain trails. with a group, you can
<1>show or hide all items with setGroupVisible()
<2>Enable or diosable all items with setGroupEnabled()
<3>specify whether all items are checked with setGroupCheckable()
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/menu_save" android:title="@string/menu_save" /> <!-- menu group --> <group android:id="@+id/group_delete"> <item android:id="@+id/menu_archive" android:title="@string/menu_archive" /> <item android:id="@+id/menu_delete" android:title="@string/menu_delete" /> </group> </menu>
6.1 Using checkable menu items
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/map" android:title="@string/map" /> <item android:id="@+id/satellite" android:title="@string/satellite" />
<item android:id="@+id/traffic"
android:title="@string/traffic" /> </group> </menu>
android:checkableBehavior attribute accpets either:
<i>single: Only one item from the group can be checked (radio buttons)
<ii>all: All items can be checked (checkboxes)
<iii>none: No items are checkable
6.2 adding Menu items Based on an Intent
6.3 Allowing youractivity to be added to other menus