Example usage for android.support.v4.view Menu getItem

List of usage examples for android.support.v4.view Menu getItem

Introduction

In this page you can find the example usage for android.support.v4.view Menu getItem.

Prototype

@Override
    MenuItem getItem(int index);

Source Link

Usage

From source file:com.actionbarsherlock.sample.hcgallery.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.getItem(1).setTitle(mToggleLabels[mLabelIndex]);
    return true;
}

From source file:com.actionbarsherlock.internal.app.ActionBarHandlerWatson.java

@Override
protected void onMenuInflated(Menu menu) {
    int maxItems = MAX_ACTION_BAR_ITEMS_PORTRAIT;
    if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        maxItems = MAX_ACTION_BAR_ITEMS_LANDSCAPE;
    }//  ww w  .j av a2 s.  c  om

    //Iterate and grab as many actions as we can up to maxItems honoring
    //their showAsAction values
    int ifItems = 0;
    final int count = menu.size();
    List<MenuItemImpl> keep = new ArrayList<MenuItemImpl>();
    for (int i = 0; i < count; i++) {
        MenuItemImpl item = (MenuItemImpl) menu.getItem(i);

        //Items without an icon or items without a title when the title
        //is enabled are forced into the normal options menu
        if (!mIsActionItemTextEnabled && (item.getIcon() == null)) {
            continue;
        } else if (mIsActionItemTextEnabled && ((item.getTitle() == null) || item.getTitle().equals(""))) {
            continue;
        }

        if ((item.getShowAsAction() & MenuItem.SHOW_AS_ACTION_ALWAYS) != 0) {
            //Show always therefore add to keep list
            keep.add(item);

            if ((keep.size() > maxItems) && (ifItems > 0)) {
                //If we have exceeded the max and there are "ifRoom" items
                //then iterate backwards to remove one and add it to the
                //head of the classic items list.
                for (int j = keep.size() - 1; j >= 0; j--) {
                    if ((keep.get(j).getShowAsAction() & MenuItem.SHOW_AS_ACTION_IF_ROOM) != 0) {
                        keep.remove(j);
                        ifItems -= 1;
                        break;
                    }
                }
            }
        } else if (((item.getShowAsAction() & MenuItem.SHOW_AS_ACTION_IF_ROOM) != 0)
                && (keep.size() < maxItems)) {
            //"ifRoom" items are added if we have not exceeded the max.
            keep.add(item);
            ifItems += 1;
        }
    }

    //Mark items that will be shown on the action bar as such so they do
    //not show up on the activity options menu
    mActionBar.removeAllItems();
    mActionBar.setIsActionItemTextEnabled(mIsActionItemTextEnabled);
    for (MenuItemImpl item : keep) {
        item.setIsShownOnActionBar(true);

        //Get a new item for this menu item
        ActionBarWatson.Item watsonItem = mActionBar.newItem();

        //Create and initialize a watson itemview wrapper
        WatsonItemViewWrapper watsonWrapper = new WatsonItemViewWrapper(watsonItem);
        watsonWrapper.initialize(item, MenuBuilder.TYPE_WATSON);

        //Associate the itemview with the item so changes will be reflected
        item.setItemView(MenuBuilder.TYPE_WATSON, watsonWrapper);

        //Add to the action bar for display
        mActionBar.addItem(watsonItem);
    }
}