android.support.v4.view
Class MenuItemCompat

java.lang.Object
  extended by android.support.v4.view.MenuItemCompat

public class MenuItemCompat
extends java.lang.Object

Helper for accessing features in MenuItem introduced after API level 4 in a backwards compatible fashion.

Note: You cannot get an instance of this class. Instead, it provides static methods that correspond to the methods in MenuItem, but take a MenuItem object as an additional argument.


Nested Class Summary
static interface MenuItemCompat.OnActionExpandListener
          Interface definition for a callback to be invoked when a menu item marked with SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW is expanded or collapsed.
 
Field Summary
static int SHOW_AS_ACTION_ALWAYS
          Always show this item as a button in an Action Bar.
static int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
          This item's action view collapses to a normal menu item.
static int SHOW_AS_ACTION_IF_ROOM
          Show this item as a button in an Action Bar if the system decides there is room for it.
static int SHOW_AS_ACTION_NEVER
          Never show this item as a button in an Action Bar.
static int SHOW_AS_ACTION_WITH_TEXT
          When this item is in the action bar, always show it with a text label even if it also has an icon specified.
 
Constructor Summary
MenuItemCompat()
           
 
Method Summary
static boolean collapseActionView(android.view.MenuItem item)
          Collapse the action view associated with this menu item.
static boolean expandActionView(android.view.MenuItem item)
          Expand the action view associated with this menu item.
static ActionProvider getActionProvider(android.view.MenuItem item)
          Gets the ActionProvider.
static android.view.View getActionView(android.view.MenuItem item)
          Returns the currently set action view for this menu item.
static boolean isActionViewExpanded(android.view.MenuItem item)
          Returns true if this menu item's action view has been expanded.
static android.view.MenuItem setActionProvider(android.view.MenuItem item, ActionProvider provider)
          Sets the ActionProvider responsible for creating an action view if the item is placed on the action bar.
static android.view.MenuItem setActionView(android.view.MenuItem item, int resId)
          Set an action view for this menu item.
static android.view.MenuItem setActionView(android.view.MenuItem item, android.view.View view)
          Set an action view for this menu item.
static android.view.MenuItem setOnActionExpandListener(android.view.MenuItem item, MenuItemCompat.OnActionExpandListener listener)
          Set an MenuItemCompat.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed.
static void setShowAsAction(android.view.MenuItem item, int actionEnum)
          Sets how this item should display in the presence of a compatible Action Bar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHOW_AS_ACTION_NEVER

public static final int SHOW_AS_ACTION_NEVER
Never show this item as a button in an Action Bar.

See Also:
Constant Field Values

SHOW_AS_ACTION_IF_ROOM

public static final int SHOW_AS_ACTION_IF_ROOM
Show this item as a button in an Action Bar if the system decides there is room for it.

See Also:
Constant Field Values

SHOW_AS_ACTION_ALWAYS

public static final int SHOW_AS_ACTION_ALWAYS
Always show this item as a button in an Action Bar. Use sparingly! If too many items are set to always show in the Action Bar it can crowd the Action Bar and degrade the user experience on devices with smaller screens. A good rule of thumb is to have no more than 2 items set to always show at a time.

See Also:
Constant Field Values

SHOW_AS_ACTION_WITH_TEXT

public static final int SHOW_AS_ACTION_WITH_TEXT
When this item is in the action bar, always show it with a text label even if it also has an icon specified.

See Also:
Constant Field Values

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
This item's action view collapses to a normal menu item. When expanded, the action view temporarily takes over a larger segment of its container.

See Also:
Constant Field Values
Constructor Detail

MenuItemCompat

public MenuItemCompat()
Method Detail

setShowAsAction

public static void setShowAsAction(android.view.MenuItem item,
                                   int actionEnum)
Sets how this item should display in the presence of a compatible Action Bar. If the given item is compatible, this will call the item's supported implementation of MenuItem#setShowAsAction(int).

Parameters:
item - - the item to change
actionEnum - - How the item should display.

setActionView

public static android.view.MenuItem setActionView(android.view.MenuItem item,
                                                  android.view.View view)
Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.

Parameters:
item - the item to change
view - View to use for presenting this item to the user.
Returns:
This Item so additional setters can be called.
See Also:
setShowAsAction(MenuItem, int)

setActionView

public static android.view.MenuItem setActionView(android.view.MenuItem item,
                                                  int resId)
Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.

Note: Setting an action view overrides the action provider set via setActionProvider(MenuItem, ActionProvider).

Parameters:
item - the item to change
resId - Layout resource to use for presenting this item to the user.
Returns:
This Item so additional setters can be called.
See Also:
setShowAsAction(MenuItem, int)

getActionView

public static android.view.View getActionView(android.view.MenuItem item)
Returns the currently set action view for this menu item.

Parameters:
item - the item to query
Returns:
This item's action view

setActionProvider

public static android.view.MenuItem setActionProvider(android.view.MenuItem item,
                                                      ActionProvider provider)
Sets the ActionProvider responsible for creating an action view if the item is placed on the action bar. The provider also provides a default action invoked if the item is placed in the overflow menu.

Note: Setting an action provider overrides the action view set via setActionView(MenuItem, View).

Parameters:
item - item to change
provider - The action provider.
Returns:
This Item so additional setters can be called.
See Also:
ActionProvider

getActionProvider

public static ActionProvider getActionProvider(android.view.MenuItem item)
Gets the ActionProvider.

Returns:
The action provider.
See Also:
ActionProvider, setActionProvider(MenuItem, ActionProvider)

expandActionView

public static boolean expandActionView(android.view.MenuItem item)
Expand the action view associated with this menu item. The menu item must have an action view set, as well as the showAsAction flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using setOnActionExpandListener(MenuItem, OnActionExpandListener) it will have its MenuItemCompat.OnActionExpandListener.onMenuItemActionExpand(MenuItem) method invoked. The listener may return false from this method to prevent expanding the action view.

Returns:
true if the action view was expanded, false otherwise.

collapseActionView

public static boolean collapseActionView(android.view.MenuItem item)
Collapse the action view associated with this menu item. The menu item must have an action view set, as well as the showAsAction flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using setOnActionExpandListener(MenuItem, android.support.v4.view.MenuItemCompat.OnActionExpandListener) it will have its MenuItemCompat.OnActionExpandListener.onMenuItemActionCollapse(MenuItem) method invoked. The listener may return false from this method to prevent collapsing the action view.

Returns:
true if the action view was collapsed, false otherwise.

isActionViewExpanded

public static boolean isActionViewExpanded(android.view.MenuItem item)
Returns true if this menu item's action view has been expanded.

Returns:
true if the item's action view is expanded, false otherwise.
See Also:
expandActionView(MenuItem), collapseActionView(MenuItem), SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW, MenuItemCompat.OnActionExpandListener

setOnActionExpandListener

public static android.view.MenuItem setOnActionExpandListener(android.view.MenuItem item,
                                                              MenuItemCompat.OnActionExpandListener listener)
Set an MenuItemCompat.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed. The menu item must be configured to expand or collapse its action view using the flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW.

Parameters:
listener - Listener that will respond to expand/collapse events
Returns:
This menu item instance for call chaining