Example usage for android.view Menu getItem

List of usage examples for android.view Menu getItem

Introduction

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

Prototype

public MenuItem getItem(int index);

Source Link

Document

Gets the menu item at the given index.

Usage

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  ww  .  java  2 s  . co  m
                MenuUtils.scanMenuShortcuts(moreSub);
            }
        }

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

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

private void onClipboardDropdown(View anchor) {
    ViewUtils.setViewsVisible(mStaticButtons, true, R.id.title_paste);
    if (anchor == null)
        anchor = ViewUtils.getFirstView(this, R.id.title_paste, R.id.title_icon, R.id.frag_holder);

    final BetterPopupWindow clipdrop = new BetterPopupWindow(this, anchor);
    View root = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.multiselect, null);
    GridView mGridCommands = (GridView) root.findViewById(R.id.multiselect_command_grid);
    final ListView mListClipboard = (ListView) root.findViewById(R.id.multiselect_item_list);
    mListClipboard.setAdapter(getClipboard());
    mListClipboard.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> list, View view, final int pos, long id) {
            //OpenPath file = mClipboard.get(pos);
            //if(file.getParent().equals(mLastPath))
            Animation anim = AnimationUtils.loadAnimation(OpenExplorer.this, R.anim.slide_out_left);
            //anim.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
            list.getChildAt(pos).startAnimation(anim);
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    getClipboard().remove(pos);
                    mListClipboard.invalidate();
                    if (getClipboard().getCount() == 0)
                        clipdrop.dismiss();
                }/*from  w ww .j a  va2s .c  o m*/
            }, anim.getDuration());
            //else
            //getEventHandler().copyFile(file, mLastPath, OpenExplorer.this);
        }
    });
    final Menu menu = IconContextMenu.newMenu(this, R.menu.multiselect);
    MenuUtils.setMenuChecked(menu, getClipboard().isMultiselect(), R.id.menu_multi);
    MenuUtils.setMenuEnabled(menu, getClipboard().hasPastable(), R.id.menu_multi_all_copy,
            R.id.menu_multi_all_copy, R.id.menu_multi_all_move);
    final IconContextMenuAdapter cmdAdapter = new IconContextMenuAdapter(this, menu);
    cmdAdapter.setTextLayout(R.layout.context_item);
    mGridCommands.setAdapter(cmdAdapter);
    mGridCommands.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> list, View view, int pos, long id) {
            MenuItem item = menu.getItem(pos);
            onClick(item.getItemId(), item, view);
            clipdrop.dismiss();
        }
    });

    float w = getResources().getDimension(R.dimen.popup_width) * (3 / 2);
    if (w > getWindowWidth())
        w = getWindowWidth() - 20;
    clipdrop.setPopupWidth((int) w);
    clipdrop.setContentView(root);

    clipdrop.showLikePopDownMenu();
    //dropdown.setAdapter(this, new IconContextMenuAdapter(context, menu))
    //BetterPopupWindow win = new BetterPopupWindow(this, anchor);
    //ListView list = ((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.multiselect, null);
    //win.setContentView(root)
}

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

public void setupBaseBarButtons(Menu menu, boolean flush) {
    if (flush)/*w  ww  .j  a  v  a  2s.  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();/* w  w  w  .  j  a  va2  s  .c om*/

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

From source file:com.rfo.basic.Run.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) { // Executed when Menu key is pressed (before onCreateOptionsMenu() above.

    super.onPrepareOptionsMenu(menu);
    MenuItem item;//from  www  .  j a  va2  s .  co m
    if (Stop) { // If program running display with Editor dimmed
        item = menu.getItem(0); // Other wise dim Stop and undim Editor
        item.setEnabled(false);
        item = menu.getItem(1);
        item.setEnabled(true);
    }
    return true;
}

From source file:com.rfo.basic.Run.java

@Override
public boolean onCreateOptionsMenu(Menu menu) { // Called when the menu key is pressed.
    super.onCreateOptionsMenu(menu);
    if (!Settings.getConsoleMenu(this)) {
        return false;
    }// w w  w  .  j  a va2s . c  o  m

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.run, menu);
    MenuItem item = menu.getItem(1);
    if (Basic.DoAutoRun) { // If APK or shortcut, menu action is "Exit", not "Editor"
        item.setTitle(getString(R.string.exit));
    }
    item.setEnabled(false);
    return true;
}