Example usage for android.view Menu size

List of usage examples for android.view Menu size

Introduction

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

Prototype

public int size();

Source Link

Document

Get the number of items in the menu.

Usage

From source file:com.plusot.senselib.SenseMain.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (menu.size() < 2)
        super.onPrepareOptionsMenu(menu);
    menuItemz = new MenuItem[menu.size()];
    for (int i = 0; i < menu.size(); i++) {
        menuItemz[i] = menu.getItem(i);/*from w w  w  .  j  av a2 s.co  m*/
    }
    //      if (SenseGlobals.activity.equals(SenseGlobals.ActivityMode.RUN)) {
    //         menuItemz[0].setVisible(false);
    //         menuItemz[1].setVisible(true);
    //      } else {
    //         menuItemz[0].setVisible(true);
    //         menuItemz[1].setVisible(false);
    //      }
    //      if (!Globals.isBikeApp) {
    //         MenuItem item = menuItems.get(MyMenu.MENU_STEPS);
    //         if (item != null) item.setVisible(false);
    //      }
    return super.onPrepareOptionsMenu(menu);
}

From source file:org.mozilla.gecko.BrowserApp.java

private static Menu findParentMenu(Menu menu, MenuItem item) {
    final int itemId = item.getItemId();

    final int count = (menu != null) ? menu.size() : 0;
    for (int i = 0; i < count; i++) {
        MenuItem menuItem = menu.getItem(i);
        if (menuItem.getItemId() == itemId) {
            return menu;
        }// w w  w.  j  a  v  a 2  s .co m
        if (menuItem.hasSubMenu()) {
            Menu parent = findParentMenu(menuItem.getSubMenu(), item);
            if (parent != null) {
                return parent;
            }
        }
    }

    return null;
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

private void handleMoreMenu(Menu menu, boolean forceFromToolbar, int max) {
    if (forceFromToolbar || (menu.size() > max && Build.VERSION.SDK_INT > 13 && getWindowWidth() < 700)) {
        MenuItem more = menu.findItem(R.id.menu_more);
        if (more != null) {
            SubMenu moreSub = more.getSubMenu();
            if (moreSub != null) {
                for (int i = Math.max(0, max - 1); i < menu.size(); i++) {
                    MenuItem item = menu.getItem(i);
                    if (item.getItemId() == R.id.menu_more)
                        continue;
                    MenuItem ni = MenuUtils.transferMenu(item, moreSub);
                    if (ni == null)
                        continue;
                    ni.setAlphabeticShortcut((char) ('a' + i));
                    item.setVisible(false);
                }//from   w  w  w.java 2 s .  c  o  m
                MenuUtils.scanMenuShortcuts(moreSub);
            }
        }

    } else
        MenuUtils.setMenuVisible(menu, false, R.id.menu_more);
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

public void setupBaseBarButtons(Menu menu, boolean flush) {
    if (flush)//  w  w w  . j a  va 2 s.c o  m
        mLastMenuClass = "";
    TableLayout mBaseBar = (TableLayout) findViewById(R.id.base_bar);
    mToolbarButtons = (ViewGroup) findViewById(R.id.base_row);
    mStaticButtons = (ViewGroup) findViewById(R.id.title_static_buttons);
    OpenFragment f = getSelectedFragment();
    boolean topButtons = false;
    if (!getResources().getBoolean(R.bool.allow_split_actionbar)
            || !(getSetting(null, "pref_basebar", true) || mBaseBar == null || mToolbarButtons == null)
                    && findViewById(R.id.title_buttons) != null) {
        mToolbarButtons = (ViewGroup) findViewById(R.id.title_buttons);
        if (mToolbarButtons == null && !BEFORE_HONEYCOMB)
            mToolbarButtons = (ViewGroup) getActionBar().getCustomView().findViewById(R.id.title_buttons);
        if (mBaseBar != null)
            mBaseBar.setVisibility(View.GONE);
        topButtons = true;
    }
    if (!shouldFlushMenu(menu))
        return;
    USE_SPLIT_ACTION_BAR = !topButtons;
    if (mToolbarButtons != null) {
        mToolbarButtons.removeAllViews();
        //if(!topButtons) mToolbarButtons.measure(LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.actionbar_compat_height));

        int i = -1;
        int btnWidth = getResources().getDimensionPixelSize(R.dimen.actionbar_compat_button_width)
                + (getResources().getDimensionPixelSize(R.dimen.vpi_padding_horizontal) * 2); // (int)(16 * getResources().getDimension(R.dimen.one_dp));
        int tblWidth = mToolbarButtons.getWidth();
        if (tblWidth <= 0 && !topButtons)
            tblWidth = getWindowWidth();
        if (topButtons || tblWidth <= 0 || tblWidth > getWindowWidth()
                || !getResources().getBoolean(R.bool.ignore_max_base_buttons))
            tblWidth = btnWidth * getResources().getInteger(R.integer.max_base_buttons);
        ArrayList<View> buttons = new ArrayList<View>();
        buttons.addAll(ViewUtils.findChildByClass(mToolbarButtons, ImageButton.class));
        boolean maxedOut = false;
        while (++i < menu.size()) {
            if (buttons.size() * btnWidth >= tblWidth) {
                maxedOut = true;
                Logger.LogDebug("Base bar full after #" + i + " ~ " + buttons.size() + " ("
                        + (buttons.size() * btnWidth) + ">" + tblWidth + ")!");
                break;
            } else if (menu.getItem(i) instanceof MenuItemImpl) {
                final MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
                //if(item.getItemId() == R.id.title_menu) break;
                if (!item.isCheckable() && item.isVisible()) {
                    View btn = makeMenuButton(item, mToolbarButtons);
                    if (item.hasSubMenu())
                        btn.setTag(item.getSubMenu());
                    else if (!BEFORE_HONEYCOMB && item.getActionView() != null) {
                        if (DEBUG)
                            Logger.LogDebug("ACTION VIEW!!!");
                        btn = item.getActionView();
                        //ActionBarHelper h = ActionBarHelper.createInstance(this);
                    }
                    buttons.add(btn);
                    if (i > 0)
                        btn.setNextFocusLeftId(menu.getItem(i - 1).getItemId());
                    if (i < menu.size() - 1)
                        btn.setNextFocusRightId(menu.getItem(i + 1).getItemId());
                    if (!USE_PRETTY_MENUS || topButtons)
                        btn.setOnCreateContextMenuListener(this);
                    menu.getItem(i).setVisible(false);
                    btn.setOnClickListener(this);
                    btn.setOnFocusChangeListener(this);
                    btn.setOnKeyListener(this);
                    if (mToolbarButtons.findViewById(menu.getItem(i).getItemId()) == null)
                        mToolbarButtons.addView(btn);
                    //menu.removeItem(item.getItemId());
                    if (DEBUG)
                        Logger.LogDebug("Added " + item.getTitle() + " to base bar.");
                }
                //else Logger.LogWarning(item.getTitle() + " should not show. " + item.getShowAsAction() + " :: " + item.getFlags());
            }
        }
        mToolbarButtons.setVisibility(View.VISIBLE);
        mLastMenuClass = f.getClassName();
        if (MenuUtils.countVisibleMenus(mMainMenu) > 0) {
            if (maxedOut && buttons.size() > 0) {
                View old = buttons.remove(buttons.size() - 1);
                MenuUtils.setMenuVisible(mMainMenu, true, old.getId());
                mToolbarButtons.removeView(old);
            }
            final ImageButton btn = (ImageButton) getLayoutInflater().inflate(R.layout.toolbar_button,
                    mToolbarButtons);
            btn.setImageResource(R.drawable.ic_menu_more);
            //btn.measure(getResources().getDimensionPixelSize(R.dimen.actionbar_compat_button_home_width), getResources().getDimensionPixelSize(R.dimen.actionbar_compat_height));
            btn.setId(R.id.menu_more);
            if (buttons.size() > 0) {
                buttons.get(buttons.size() - 1).setNextFocusRightId(R.id.menu_more);
                btn.setNextFocusLeftId(buttons.get(buttons.size() - 1).getId());
            }
            btn.setOnKeyListener(this);
            btn.setOnClickListener(this);
            btn.setOnLongClickListener(this);
            btn.setFocusable(true);
            btn.setOnFocusChangeListener(this);
            buttons.add(btn);
            mToolbarButtons.addView(btn);
        }
        if (buttons.size() > 0) {
            View last = buttons.get(buttons.size() - 1);
            last.setNextFocusRightId(android.R.id.home);
            if (findViewById(android.R.id.home) != null)
                findViewById(android.R.id.home).setNextFocusLeftId(last.getId());
        }

        Logger.LogDebug("Added " + buttons.size() + " children to Base Bar.");
        if (mBaseBar != null) {
            if (buttons.size() < 1)
                mBaseBar.setVisibility(View.GONE);
            else
                mBaseBar.setStretchAllColumns(true);
        }
    } else if (BEFORE_HONEYCOMB)
        Logger.LogWarning("No Base Row!?");
}

From source file:org.thoughtcrime.securesms.conversation.ConversationActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = this.getMenuInflater();
    menu.clear();//from  w w w. j  a  v a 2 s .  c  o  m

    if (isSecureText) {
        if (recipient.getExpireMessages() > 0) {
            inflater.inflate(R.menu.conversation_expiring_on, menu);

            final MenuItem item = menu.findItem(R.id.menu_expiring_messages);
            final View actionView = MenuItemCompat.getActionView(item);
            final TextView badgeView = actionView.findViewById(R.id.expiration_badge);

            badgeView.setText(
                    ExpirationUtil.getExpirationAbbreviatedDisplayValue(this, recipient.getExpireMessages()));
            actionView.setOnClickListener(v -> onOptionsItemSelected(item));
        } else {
            inflater.inflate(R.menu.conversation_expiring_off, menu);
        }
    }

    if (isSingleConversation()) {
        if (isSecureText)
            inflater.inflate(R.menu.conversation_callable_secure, menu);
        else
            inflater.inflate(R.menu.conversation_callable_insecure, menu);
    } else if (isGroupConversation()) {
        inflater.inflate(R.menu.conversation_group_options, menu);

        if (!isPushGroupConversation()) {
            inflater.inflate(R.menu.conversation_mms_group_options, menu);
            if (distributionType == ThreadDatabase.DistributionTypes.BROADCAST) {
                menu.findItem(R.id.menu_distribution_broadcast).setChecked(true);
            } else {
                menu.findItem(R.id.menu_distribution_conversation).setChecked(true);
            }
        } else if (isActiveGroup()) {
            inflater.inflate(R.menu.conversation_push_group_options, menu);
        }
    }

    inflater.inflate(R.menu.conversation, menu);

    if (isSingleConversation() && isSecureText) {
        inflater.inflate(R.menu.conversation_secure, menu);
    } else if (isSingleConversation()) {
        inflater.inflate(R.menu.conversation_insecure, menu);
    }

    if (recipient != null && recipient.isMuted())
        inflater.inflate(R.menu.conversation_muted, menu);
    else
        inflater.inflate(R.menu.conversation_unmuted, menu);

    if (isSingleConversation() && getRecipient().getContactUri() == null) {
        inflater.inflate(R.menu.conversation_add_to_contacts, menu);
    }

    if (recipient != null && recipient.isLocalNumber()) {
        if (isSecureText)
            menu.findItem(R.id.menu_call_secure).setVisible(false);
        else
            menu.findItem(R.id.menu_call_insecure).setVisible(false);

        MenuItem muteItem = menu.findItem(R.id.menu_mute_notifications);

        if (muteItem != null) {
            muteItem.setVisible(false);
        }
    }

    searchViewItem = menu.findItem(R.id.menu_search);

    SearchView searchView = (SearchView) searchViewItem.getActionView();
    SearchView.OnQueryTextListener queryListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchViewModel.onQueryUpdated(query, threadId);
            searchNav.showLoading();
            fragment.onSearchQueryUpdated(query);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            searchViewModel.onQueryUpdated(query, threadId);
            searchNav.showLoading();
            fragment.onSearchQueryUpdated(query);
            return true;
        }
    };

    searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            searchView.setOnQueryTextListener(queryListener);
            searchViewModel.onSearchOpened();
            searchNav.setVisibility(View.VISIBLE);
            searchNav.setData(0, 0);
            inputPanel.setVisibility(View.GONE);

            for (int i = 0; i < menu.size(); i++) {
                if (!menu.getItem(i).equals(searchViewItem)) {
                    menu.getItem(i).setVisible(false);
                }
            }
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            searchView.setOnQueryTextListener(null);
            searchViewModel.onSearchClosed();
            searchNav.setVisibility(View.GONE);
            inputPanel.setVisibility(View.VISIBLE);
            fragment.onSearchQueryUpdated(null);
            invalidateOptionsMenu();
            return true;
        }
    });

    super.onPrepareOptionsMenu(menu);
    return true;
}