English | Site Directory

Android - An Open Handset Alliance Project

android.widget
public class

android.widget.MenuBuilder

java.lang.Object
android.widget.MenuBuilder ContextMenu Menu

Implementation of the Menu interface for creating a standard menu UI.

Nested Classes
MenuBuilder.Callback Called by menu to notify of close and selection changes  
MenuBuilder.ItemInvoker Called by menu items to execute their associated action  
Known Direct Subclasses

Summary

Constants

      Value  
int  NUM_TYPES  The number of different menu types   0x00000004 
int  TYPE_CONTEXT  The menu type that represents the context menu view   0x00000003 
int  TYPE_EXPANDED  The menu type that represents the expanded menu view   0x00000001 
int  TYPE_ICON  The menu type that represents the icon menu view   0x00000000 
int  TYPE_SUB  The menu type that represents the sub menu view   0x00000002 
Constants inherited from interface android.view.Menu

Public Constructors

          MenuBuilder(Context context)

Public Methods

        Item  add(int group, int id, CharSequence title, Runnable callback)
Add a new item to the menu.
        Item  add(int group, int id, CharSequence title, int iconResId)
Add a new item to the menu.
        Item  add(int group, int id, int titleRes, int iconResId)
Add a new item to the menu.
        Item  add(int group, int id, CharSequence title)
Add a new item to the menu.
        Item  add(int group, int id, int title)
Variation on add(int, int, CharSequence) that takes a string resource identifier instead of the string itself.
        Item  add(int group, int id, int title, Runnable callback)
Variation on add(int, int, CharSequence, Runnable) that takes a string resource identifier instead of the string itself.
        int  addIntentOptions(int group, int id, ComponentName caller, Intent[] specifics, Intent intent, int flags, Item[] outSpecificItems)
Add a group of menu items corresponding to actions that can be performed for a particular Intent.
        SubMenu  addSubMenu(int group, int id, CharSequence title)
Add a new sub-menu to the menu.
        SubMenu  addSubMenu(int group, int id, CharSequence title, int iconRes)
Add a new sub-menu to the menu.
        SubMenu  addSubMenu(int group, int id, int titleRes, int iconRes)
Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself and a resource identifier for the icon.
        SubMenu  addSubMenu(int group, int id, int titleRes, Drawable icon)
Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself, and a Drawable for the icon.
        SubMenu  addSubMenu(int group, int id, int title)
Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.
        SubMenu  addSubMenu(int group, int id, CharSequence title, Drawable icon)
Add a new sub-menu to the menu.
        void  clear()
Remove all existing items from the menu, leaving it empty as if it had just been created.
        void  clearMenuViews()
Clears the cached menu views.
        int  findGroupIndex(int group)
Get the index of the first menu item that is in the given group.
        Item  findItem(int id)
Return the menu item with a particular identifier.
        int  findItemIndex(int id)
Get the index of a menu item that has the given identifier.
        Item  get(int index)
Retrieve the menu item object at a particular index.
        View  getMenuView(int menuType, ViewGroup parent, boolean createIfNonexistent)
Gets a menu View that contains this menu's items.
        boolean  hasShownItems()
Return whether the menu currently has item items that are being shown.
        boolean  isShortcutKey(int keyCode, KeyEvent event)
Is a keypress one of the defined shortcut keys for this window.
        boolean  isShortcutsShown()
        boolean  performIdentifierAction(int id, int flags)
Execute the menu item action associated with the given menu identifier.
        boolean  performShortcut(int keyCode, KeyEvent event, int flags)
Execute the menu item action associated with the given shortcut character.
        void  removeGroup(int group)
Remove all items in the given group.
        void  removeItem(int id)
Remove the item with the given identifier.
        void  removeItemAt(int index)
Remove the item at the given index.
        void  setCallback(Callback callback)
        void  setCurrentMenuInfo(Object menuInfo)
Sets the current menu info that is set on all items added to this menu (until this is called again with different menu info, in which case that one will be added to all subsequent item additions).
        void  setDefaultItem(int id)
Set the item that is initially selected when the menu is displayed.
        void  setGroupCheckable(int group, boolean checkable, boolean exclusive)
Control whether a particular group of items can show a check mark.
        void  setGroupShown(int group, boolean shown)
Show or hide all menu items that are in the given group.
        void  setHeader(int titleRes)
Sets the header of the context menu to the title given in titleRes resource identifier.
        void  setHeader(int titleRes, Drawable icon)
Sets the header of the context menu to the title given in titleRes resource identifier and the icon given in icon Drawable.
        void  setHeader(int titleRes, int iconRes)
Sets the header of the context menu to the title given in titleRes resource identifier and the icon given in iconRes resource id.
        void  setHeader(CharSequence title)
Sets the header of the context menu to the title given in title.
        void  setHeader(View headerView)
Sets the header of the context menu to the View given in headerView.
        void  setHeader(CharSequence title, int iconRes)
Sets the header of the context menu to the title given in title and the icon given in iconRes resource id.
        void  setHeader(CharSequence title, Drawable icon)
Sets the header of the context menu to the title given in title and the icon given in icon Drawable.
        void  setItemCheckable(int id, boolean checkable)
Control whether a particular item with the given identifier can display a check mark.
        void  setItemChecked(int id, boolean checked)
Show or hide the check mark for a particular menu item selected by its identifier.
        void  setItemShown(int id, boolean shown)
Show or hide a particular menu item selected by its identifier.
        void  setQwertyMode(boolean isQwerty)
Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).
        void  setShortcutsShown(boolean shortcutsShown)
Sets whether the shortcuts should be shown on menus.
        int  size()
Get the number of items in the menu.
Methods inherited from class java.lang.Object
Methods inherited from interface android.view.ContextMenu
Methods inherited from interface android.view.Menu

Details

Constants

public static final int NUM_TYPES

The number of different menu types
Constant Value: 4 (0x00000004)

public static final int TYPE_CONTEXT

The menu type that represents the context menu view
Constant Value: 3 (0x00000003)

public static final int TYPE_EXPANDED

The menu type that represents the expanded menu view
Constant Value: 1 (0x00000001)

public static final int TYPE_ICON

The menu type that represents the icon menu view
Constant Value: 0 (0x00000000)

public static final int TYPE_SUB

The menu type that represents the sub menu view
Constant Value: 2 (0x00000002)

Public Constructors

public MenuBuilder(Context context)

Public Methods

public Item add(int group, int id, CharSequence title, Runnable callback)

Add a new item to the menu. This item displays the given title for its label and will invoke callback when it is selected.

public Item add(int group, int id, CharSequence title, int iconResId)

Add a new item to the menu. This item displays the given title for its label. Also displays the given iconRes (which is converted to a Drawable only when needed).

public Item add(int group, int id, int titleRes, int iconResId)

Add a new item to the menu. This item displays the given titleRes resource for its label. Also displays the given iconRes (which is converted to a Drawable only when needed).

public Item add(int group, int id, CharSequence title)

Add a new item to the menu. This item displays the given title for its label.

public Item add(int group, int id, int title)

Variation on add(int, int, CharSequence) that takes a string resource identifier instead of the string itself.

public Item add(int group, int id, int title, Runnable callback)

Variation on add(int, int, CharSequence, Runnable) that takes a string resource identifier instead of the string itself.

public int addIntentOptions(int group, int id, ComponentName caller, Intent[] specifics, Intent intent, int flags, Item[] outSpecificItems)

Add a group of menu items corresponding to actions that can be performed for a particular Intent. The Intent is most often configured with a null action, the data that the current activity is working with, and includes either the ALTERNATIVE_CATEGORY or SELECTED_ALTERNATIVE_CATEGORY to find activities that have said they would like to be included as optional action. You can, however, use any Intent you want.

See queryIntentActivityOptions(ComponentName, Intent[], Intent, int) for more * details on the caller, specifics, and intent arguments. The list returned by that function is used to populate the resulting menu items.

All of the menu items of possible options for the intent will be added with the given group and id. You can use the group to control ordering of the items in relation to other items in the menu. Normally this function will automatically remove any existing items in the menu in the same group and place a divider above and below the added items; this behavior can be modified with the flags parameter. For each of the generated items setIntent(Intent) is called to associate the appropriate Intent with the item; this means the activity will automatically be started for you without having to do anything else.

public SubMenu addSubMenu(int group, int id, CharSequence title)

Add a new sub-menu to the menu. This item displays the given title for its label. The system will call the activity's onCreatePanelMenu(int, Menu) callback method with a SubMenu object and the id you supply here. Note that you can only have one level of sub-menus, i.e. you cannnot add a subMenu to a subMenu: An UnsupportedOperationException will be thrown if you try.

public SubMenu addSubMenu(int group, int id, CharSequence title, int iconRes)

Add a new sub-menu to the menu. This item displays the given title for its label and iconRes as the icon. The system will call the activity's onCreatePanelMenu(int, Menu) callback method with a SubMenu object and the id you supply here. Note that you can only have one level of sub-menus, i.e. you cannnot add a subMenu to a subMenu: An UnsupportedOperationException will be thrown if you try.

public SubMenu addSubMenu(int group, int id, int titleRes, int iconRes)

Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself and a resource identifier for the icon.

public SubMenu addSubMenu(int group, int id, int titleRes, Drawable icon)

Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself, and a Drawable for the icon.

public SubMenu addSubMenu(int group, int id, int title)

Variation on addSubMenu(int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.

public SubMenu addSubMenu(int group, int id, CharSequence title, Drawable icon)

Add a new sub-menu to the menu. This item displays the given title for its label and iconRes as the icon. The system will call the activity's onCreatePanelMenu(int, Menu) callback method with a SubMenu object and the id you supply here. Note that you can only have one level of sub-menus, i.e. you cannnot add a subMenu to a subMenu: An UnsupportedOperationException will be thrown if you try.

public void clear()

Remove all existing items from the menu, leaving it empty as if it had just been created.

public void clearMenuViews()

Clears the cached menu views. Call this if the menu views need to another layout (for example, if the screen size has changed).

public int findGroupIndex(int group)

Get the index of the first menu item that is in the given group. Note that this index can change any time items are added or removed from the menu; you should not keep it around. If you want to hold on to an item for later access, hold on to the Item object.

public Item findItem(int id)

Return the menu item with a particular identifier.

public int findItemIndex(int id)

Get the index of a menu item that has the given identifier. Note that this index can change any time items are added or removed from the menu; you should not keep it around. If you want to hold on to an item for later access, hold on to the Item object.

public Item get(int index)

Retrieve the menu item object at a particular index. Use with findItemIndex() and size().

public View getMenuView(int menuType, ViewGroup parent, boolean createIfNonexistent)

Gets a menu View that contains this menu's items.

Parameters

menuType The type of menu to get a View for (must be one of TYPE_ICON, TYPE_EXPANDED, TYPE_SUB, TYPE_CONTEXT).
parent The ViewGroup that provides a set of LayoutParams values for this menu view
createIfNonexistent If the menu View hasn't been created before, create and return the menu View (true), or return null (false)

Returns

  • A View for the menu of type menuType

public boolean hasShownItems()

Return whether the menu currently has item items that are being shown.

public boolean isShortcutKey(int keyCode, KeyEvent event)

Is a keypress one of the defined shortcut keys for this window.

public boolean isShortcutsShown()

Returns

  • Whether shortcuts should be shown on menus.

public boolean performIdentifierAction(int id, int flags)

Execute the menu item action associated with the given menu identifier.

public boolean performShortcut(int keyCode, KeyEvent event, int flags)

Execute the menu item action associated with the given shortcut character.

public void removeGroup(int group)

Remove all items in the given group.

public void removeItem(int id)

Remove the item with the given identifier.

public void removeItemAt(int index)

Remove the item at the given index.

public void setCallback(Callback callback)

public void setCurrentMenuInfo(Object menuInfo)

Sets the current menu info that is set on all items added to this menu (until this is called again with different menu info, in which case that one will be added to all subsequent item additions).

Parameters

menuInfo The extra menu information to add.

public void setDefaultItem(int id)

Set the item that is initially selected when the menu is displayed.

public void setGroupCheckable(int group, boolean checkable, boolean exclusive)

Control whether a particular group of items can show a check mark. This is similar to calling setCheckable(boolean) on all of the menu items with the given group identifier, but in addition you can control whether this group contains a mutually-exclusive set items. This should be called after the items of the group have been added to the menu.

public void setGroupShown(int group, boolean shown)

Show or hide all menu items that are in the given group.

public void setHeader(int titleRes)

Sets the header of the context menu to the title given in titleRes resource identifier.

public void setHeader(int titleRes, Drawable icon)

Sets the header of the context menu to the title given in titleRes resource identifier and the icon given in icon Drawable.

public void setHeader(int titleRes, int iconRes)

Sets the header of the context menu to the title given in titleRes resource identifier and the icon given in iconRes resource id.

public void setHeader(CharSequence title)

Sets the header of the context menu to the title given in title.

public void setHeader(View headerView)

Sets the header of the context menu to the View given in headerView.

public void setHeader(CharSequence title, int iconRes)

Sets the header of the context menu to the title given in title and the icon given in iconRes resource id.

public void setHeader(CharSequence title, Drawable icon)

Sets the header of the context menu to the title given in title and the icon given in icon Drawable.

public void setItemCheckable(int id, boolean checkable)

Control whether a particular item with the given identifier can display a check mark. This is the same as looking up the Menu.Item and calling setCheckable(boolean) on it.

public void setItemChecked(int id, boolean checked)

Show or hide the check mark for a particular menu item selected by its identifier. If this item is a member of a group that contains mutually-exclusive items (set via setGroupCheckable(int, boolean, boolean), the other items in the group will be unchecked. This is the same as looking up the Menu.Item and calling setChecked(boolean) on it.

public void setItemShown(int id, boolean shown)

Show or hide a particular menu item selected by its identifier.

public void setQwertyMode(boolean isQwerty)

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).

public void setShortcutsShown(boolean shortcutsShown)

Sets whether the shortcuts should be shown on menus.

Parameters

shortcutsShown Whether shortcuts should be shown (if true and a menu item does not have a shortcut defined, that item will still NOT show a shortcut)

public int size()

Get the number of items in the menu. Note that this will change any times items are added or removed from the menu.
Build m5-rc15i -