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

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

Introduction

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

Prototype

public int size();

Source Link

Document

Get the number of items in the menu.

Usage

From source file:com.todoroo.astrid.actfm.TagUpdatesFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (menu.size() > 0)
        return;//from  ww  w . ja  v  a  2 s . c  o  m

    MenuItem item;
    if (actFmPreferenceService.isLoggedIn()) {
        item = menu.add(Menu.NONE, MENU_REFRESH_ID, Menu.NONE, R.string.ENA_refresh_comments);
        item.setIcon(R.drawable.icn_menu_refresh_dark);
    }
}

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;
    }/*from  ww w . j  a  v a  2  s.  co m*/

    //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);
    }
}