Example usage for android.support.v4.view MenuItemCompat SHOW_AS_ACTION_NEVER

List of usage examples for android.support.v4.view MenuItemCompat SHOW_AS_ACTION_NEVER

Introduction

In this page you can find the example usage for android.support.v4.view MenuItemCompat SHOW_AS_ACTION_NEVER.

Prototype

int SHOW_AS_ACTION_NEVER

To view the source code for android.support.v4.view MenuItemCompat SHOW_AS_ACTION_NEVER.

Click Source Link

Document

Never show this item as a button in an Action Bar.

Usage

From source file:gov.wa.wsdot.android.wsdot.ui.TrafficMapActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();/*  w w  w .j  av a  2 s  .co m*/
    getMenuInflater().inflate(R.menu.traffic, menu);

    if (showCameras) {
        menu.getItem(0).setTitle("Hide Cameras");
    } else {
        menu.getItem(0).setTitle("Show Cameras");
    }

    /**
     * Check if current location is within a lat/lon bounding box surrounding
     * the greater Seattle area.
     */
    try {
        LatLng center = map.getCameraPosition().target;

        if (inPolygon(seattleArea, center.latitude, center.longitude)) {
            MenuItem menuItem_Alerts = menu.add(0, MENU_ITEM_SEATTLE_ALERTS, menu.size(), "Seattle Alerts")
                    .setIcon(R.drawable.ic_menu_alerts);

            MenuItemCompat.setShowAsAction(menuItem_Alerts,
                    MenuItemCompat.SHOW_AS_ACTION_IF_ROOM | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);

            MenuItem menuItem_Lanes = menu.add(0, MENU_ITEM_EXPRESS_LANES, menu.size(), "Express Lanes");
            MenuItemCompat.setShowAsAction(menuItem_Lanes, MenuItemCompat.SHOW_AS_ACTION_NEVER);
        }

    } catch (NullPointerException e) {
        Log.e(TAG, "Error getting LatLng center");
    }

    return super.onPrepareOptionsMenu(menu);
}

From source file:org.totschnig.myexpenses.activity.MyExpenses.java

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(MyApplication.getThemeId());
    Resources.Theme theme = getTheme();
    TypedValue value = new TypedValue();
    theme.resolveAttribute(R.attr.colorAggregate, value, true);
    colorAggregate = value.data;//w  w  w . j  a v  a2 s  .  c o  m
    int prev_version = PrefKey.CURRENT_VERSION.getInt(-1);
    if (prev_version == -1) {
        //prevent preference change listener from firing when preference file is created
        if (MyApplication.getInstance().isInstrumentationTest()) {
            PreferenceManager.setDefaultValues(this, MyApplication.getTestId(), Context.MODE_PRIVATE,
                    R.xml.preferences, true);
        } else {
            PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        }
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    adHandler = new AdHandler(this);
    adHandler.init();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (StickyListHeadersListView) findViewById(R.id.left_drawer);
    mToolbar = setupToolbar(false);
    mToolbar.addView(getLayoutInflater().inflate(R.layout.custom_title, mToolbar, false));
    if (mDrawerLayout != null) {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open,
                R.string.drawer_close) {

            /**
             * Called when a drawer has settled in a completely closed state.
             */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                TransactionList tl = getCurrentFragment();
                if (tl != null)
                    tl.onDrawerClosed();
                //ActivityCompat.invalidateOptionsMenu(MyExpenses.this); // creates call to onPrepareOptionsMenu()
            }

            /**
             * Called when a drawer has settled in a completely open state.
             */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                TransactionList tl = getCurrentFragment();
                if (tl != null)
                    tl.onDrawerOpened();
                //ActivityCompat.invalidateOptionsMenu(MyExpenses.this); // creates call to onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, 0); // this disables the animation
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.addDrawerListener(mDrawerToggle);
    }
    String[] from = new String[] { KEY_DESCRIPTION, KEY_LABEL, KEY_OPENING_BALANCE, KEY_SUM_INCOME,
            KEY_SUM_EXPENSES, KEY_SUM_TRANSFERS, KEY_CURRENT_BALANCE, KEY_TOTAL, KEY_CLEARED_TOTAL,
            KEY_RECONCILED_TOTAL };
    // and an array of the fields we want to bind those fields to
    int[] to = new int[] { R.id.description, R.id.label, R.id.opening_balance, R.id.sum_income,
            R.id.sum_expenses, R.id.sum_transfer, R.id.current_balance, R.id.total, R.id.cleared_total,
            R.id.reconciled_total };
    mDrawerListAdapter = new MyGroupedAdapter(this, R.layout.account_row, null, from, to, 0);

    Toolbar accountsMenu = (Toolbar) findViewById(R.id.accounts_menu);
    accountsMenu.setTitle(R.string.pref_manage_accounts_title);
    accountsMenu.inflateMenu(R.menu.accounts);
    accountsMenu.inflateMenu(R.menu.sort);

    //Sort submenu
    MenuItem menuItem = accountsMenu.getMenu().findItem(R.id.SORT_COMMAND);
    MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_NEVER);
    sortMenu = menuItem.getSubMenu();
    sortMenu.findItem(R.id.SORT_CUSTOM_COMMAND).setVisible(true);

    //Grouping submenu
    SubMenu groupingMenu = accountsMenu.getMenu().findItem(R.id.GROUPING_ACCOUNTS_COMMAND).getSubMenu();
    AccountGrouping accountGrouping;
    try {
        accountGrouping = AccountGrouping.valueOf(PrefKey.ACCOUNT_GROUPING.getString("TYPE"));
    } catch (IllegalArgumentException e) {
        accountGrouping = AccountGrouping.TYPE;
    }
    MenuItem activeItem;
    switch (accountGrouping) {
    case CURRENCY:
        activeItem = groupingMenu.findItem(R.id.GROUPING_ACCOUNTS_CURRENCY_COMMAND);
        break;
    case NONE:
        activeItem = groupingMenu.findItem(R.id.GROUPING_ACCOUNTS_NONE_COMMAND);
        break;
    default:
        activeItem = groupingMenu.findItem(R.id.GROUPING_ACCOUNTS_TYPE_COMMAND);
        break;
    }
    activeItem.setChecked(true);

    accountsMenu.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return handleSortOption(item) || handleAccountsGrouping(item)
                    || dispatchCommand(item.getItemId(), null);
        }
    });

    mDrawerList.setAdapter(mDrawerListAdapter);
    mDrawerList.setAreHeadersSticky(false);
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mAccountId != id) {
                moveToPosition(position);
                ((SimpleCursorAdapter) mDrawerListAdapter).notifyDataSetChanged();
                closeDrawer();
            }
        }
    });

    requireFloatingActionButtonWithContentDescription(Utils.concatResStrings(this, ". ",
            R.string.menu_create_transaction, R.string.menu_create_transfer, R.string.menu_create_split));
    if (prev_version == -1) {
        getSupportActionBar().hide();
        initialSetup();
        return;
    }
    if (savedInstanceState != null) {
        mExportFormat = savedInstanceState.getString("exportFormat");
        mAccountId = savedInstanceState.getLong(KEY_ACCOUNTID, 0L);
    } else {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mAccountId = Utils.getFromExtra(extras, KEY_ROWID, 0);
            idFromNotification = extras.getLong(KEY_TRANSACTIONID, 0);
            //detail fragment from notification should only be shown upon first instantiation from notification
            if (idFromNotification != 0) {
                FragmentManager fm = getSupportFragmentManager();
                if (fm.findFragmentByTag(TransactionDetailFragment.class.getName()) == null) {
                    TransactionDetailFragment.newInstance(idFromNotification).show(fm,
                            TransactionDetailFragment.class.getName());
                    getIntent().removeExtra(KEY_TRANSACTIONID);
                }
            }
        }
    }
    if (mAccountId == 0)
        mAccountId = PrefKey.CURRENT_ACCOUNT.getLong(0L);
    setup();
}

From source file:de.uni.stuttgart.informatik.ToureNPlaner.UI.Activities.MapScreen.MapScreen.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.algorithm_constraints)
            .setVisible(!session.getSelectedAlgorithm().getConstraintTypes().isEmpty());
    if (instantRequest == MapScreenPreferences.Instant.NEVER) {
        MenuItemCompat.setShowAsAction(menu.findItem(R.id.calculate), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    } else {//from   w ww . j a  v  a 2 s  . co  m
        MenuItemCompat.setShowAsAction(menu.findItem(R.id.calculate), MenuItemCompat.SHOW_AS_ACTION_NEVER);
    }

    menu.findItem(R.id.gps).setVisible(gpsListener.isEnabled());
    menu.findItem(R.id.gps)
            .setIcon(gpsListener.isFollowing() ? R.drawable.location_enabled : R.drawable.location_disabled);
    return true;
}

From source file:org.cobaltians.cobalt.activities.CobaltActivity.java

protected void addMenuItem(Menu menu, int order, JSONObject action, final int id, String position,
        String barsColor) {//w  ww .j a v  a  2  s  . co  m
    try {
        final String name = action.getString(Cobalt.kActionName);
        String title = action.getString(Cobalt.kActionTitle);
        boolean visible = action.optBoolean(Cobalt.kActionVisible, true);
        boolean enabled = action.optBoolean(Cobalt.kActionEnabled, true);

        final MenuItem menuItem = menu.add(Menu.NONE, id, order, title);

        int showAsAction = MenuItemCompat.SHOW_AS_ACTION_IF_ROOM;
        switch (position) {
        case Cobalt.kPositionBottom:
            showAsAction = MenuItemCompat.SHOW_AS_ACTION_ALWAYS;
            break;
        case Cobalt.kPositionOverflow:
            showAsAction = MenuItemCompat.SHOW_AS_ACTION_NEVER;
            break;
        }
        MenuItemCompat.setShowAsAction(menuItem, showAsAction);

        ActionViewMenuItem actionView = new ActionViewMenuItem(this, action, barsColor);
        actionView.setActionViewMenuItemListener(new WeakReference<>(this));

        MenuItemCompat.setActionView(menuItem, actionView);
        menuItem.setVisible(visible);
        menuItem.setEnabled(enabled);
        mMenuItemsHashMap.put(name, actionView);
        //need this next hashmap to send onPressed when item is on overflow
        mMenuItemsIdMap.put(id, name);
        //need this next hashmap to set menuItem
        mMenuItemByNameMap.put(name, menuItem);
    } catch (JSONException exception) {
        if (Cobalt.DEBUG) {
            Log.w(Cobalt.TAG,
                    TAG + "addMenuItem: action " + action.toString() + " format not supported, use at least {\n"
                            + "\tname: \"name\",\n" + "\ttitle: \"title\",\n" + "}");
        }

        exception.printStackTrace();
    }
}

From source file:com.vuze.android.remote.fragment.TorrentListFragment.java

private void setupActionModeCallback() {
    mActionModeCallbackV7 = new Callback() {

        // Called when the action mode is created; startActionMode() was called
        @Override/*from  w w w .  j ava  2 s  . c  o  m*/
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onCreateActionMode");
            }

            if (mode == null && torrentListAdapter.getCheckedItemCount() == 0
                    && torrentListAdapter.getSelectedPosition() < 0) {
                return false;
            }

            Menu origMenu = menu;
            if (tb != null) {
                menu = tb.getMenu();
            }
            if (mode != null) {
                mActionMode = (mode instanceof ActionModeWrapperV7) ? mode
                        : new ActionModeWrapperV7(mode, tb, getActivity());

                mActionMode.setTitle(R.string.context_torrent_title);
            }
            ActionBarToolbarSplitter.buildActionBar(getActivity(), this, R.menu.menu_context_torrent_details,
                    menu, tb);

            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                frag.onCreateActionMode(mode, menu);
            }

            if (sideListArea == null) {
                SubMenu subMenu = origMenu.addSubMenu(R.string.menu_global_actions);
                subMenu.setIcon(R.drawable.ic_menu_white_24dp);
                MenuItemCompat.setShowAsAction(subMenu.getItem(), MenuItemCompat.SHOW_AS_ACTION_NEVER);

                try {
                    // Place "Global" actions on top bar in collapsed menu
                    MenuInflater mi = mode == null ? getActivity().getMenuInflater() : mode.getMenuInflater();
                    mi.inflate(R.menu.menu_torrent_list, subMenu);
                    onPrepareOptionsMenu(subMenu);
                } catch (UnsupportedOperationException e) {
                    Log.e(TAG, e.getMessage());
                    menu.removeItem(subMenu.getItem().getItemId());
                }
            }

            if (AndroidUtils.usesNavigationControl()) {
                MenuItem add = origMenu.add(R.string.select_multiple_items);
                add.setCheckable(true);
                add.setChecked(torrentListAdapter.isMultiCheckMode());
                add.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        boolean turnOn = !torrentListAdapter.isMultiCheckModeAllowed();

                        torrentListAdapter.setMultiCheckModeAllowed(turnOn);
                        if (turnOn) {
                            torrentListAdapter.setMultiCheckMode(turnOn);
                            torrentListAdapter.setItemChecked(torrentListAdapter.getSelectedPosition(), true);
                        }
                        return true;
                    }
                });
            }

            return true;
        }

        // Called each time the action mode is shown. Always called after
        // onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "MULTI:onPrepareActionMode " + mode);
            }
            if (tb != null) {
                menu = tb.getMenu();
            }

            // Must be called first, because our drawer sets all menu items
            // visible.. :(
            getActivity().onPrepareOptionsMenu(menu);

            prepareContextMenu(menu);

            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                frag.onPrepareActionMode(mode, menu);
            }

            AndroidUtils.fixupMenuAlpha(menu);

            return true;
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onActionItemClicked " + item.getTitle());
            }

            if (TorrentListFragment.this.handleFragmentMenuItems(item.getItemId())) {
                return true;
            }
            if (getActivity().onOptionsItemSelected(item)) {
                return true;
            }
            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                if (frag.onActionItemClicked(mode, item)) {
                    return true;
                }
            }
            return false;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onDestroyActionMode. BeingReplaced?" + actionModeBeingReplaced);
            }

            mActionMode = null;

            if (!actionModeBeingReplaced) {
                listview.post(new Runnable() {
                    @Override
                    public void run() {
                        torrentListAdapter.setMultiCheckMode(false);
                        torrentListAdapter.clearChecked();
                        updateCheckedIDs();
                    }
                });

                listview.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.actionModeBeingReplacedDone();
                        }
                    }
                });

                listview.setLongClickable(true);
                listview.requestLayout();
                AndroidUtils.invalidateOptionsMenuHC(getActivity(), mActionMode);
            }
        }
    };
}

From source file:org.de.jmg.learn.MainActivity.java

private void _SetShowAsAction(final MenuItem m) {

    final View tb = this.findViewById(R.id.action_bar);
    int SizeOther = 0;
    int width;//from w ww.j  av a  2s  .  c  o m
    ActionMenu = null;
    if (tb != null) {
        width = tb.getWidth();
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        double height = metrics.heightPixels;
        int viewTop = this.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        double scale = (height - viewTop) / (double) 950;

        if (scale < .5f) {
            isSmallDevice = true;
        }
        double ActionBarHeight = tb.getHeight();
        if (isSmallDevice && ActionBarHeight / height > .15f) {
            try {
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) tb.getLayoutParams();
                layoutParams.height = (int) (height * .15f);

                tb.setMinimumHeight((int) (height * .15f));
                tb.setLayoutParams(layoutParams);
                tb.requestLayout();
            } catch (Exception ex) {
                Log.e("SetToolbarHeight", ex.getMessage(), ex);
            }
        }
        if (width > 0) {
            ViewGroup g = (ViewGroup) tb;
            for (int i = 0; i < g.getChildCount(); i++) {
                View v = g.getChildAt(i);
                if ((v instanceof android.support.v7.widget.ActionMenuView)) {
                    SizeOther += v.getWidth();
                    ActionMenu = (android.support.v7.widget.ActionMenuView) v;
                }
            }
            if (SizeOther < width * .7)
                _blnReverse = true;
            if ((_blnReverse || SizeOther > width * .7) && ActionMenu != null) {
                if (_blnReverse) {
                    //if (!_hasBeenDownsized || _hasBeenDownsized)
                    //{
                    MenuBuilder mm = (MenuBuilder) ActionMenu.getMenu();
                    int Actions = mm.getActionItems().size();
                    try {
                        MenuItem mmm = ActionMenu.getMenu().getItem(Actions + _invisibleCount);
                        if (mmm.isVisible()) {
                            MenuItemCompat.setShowAsAction(mmm, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
                        } else {
                            _invisibleCount += 1;
                            _SetShowAsAction(mmm);
                        }
                    } catch (IndexOutOfBoundsException ex) {
                        return;
                    }
                    //}

                } else {
                    MenuItemCompat.setShowAsAction(m, MenuItemCompat.SHOW_AS_ACTION_NEVER);
                    _hasBeenDownsized = true;
                }
                ActionMenu.getViewTreeObserver()
                        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                if (ActionMenu != null) {
                                    lib.removeLayoutListener(ActionMenu.getViewTreeObserver(), this);
                                    int SizeNew = ActionMenu.getWidth();
                                    Log.v("Test", "" + SizeNew);
                                    MenuBuilder mm = (MenuBuilder) ActionMenu.getMenu();
                                    int count = mm.getActionItems().size();
                                    if (count >= 1 && !(_blnReverse && SizeNew > tb.getWidth() * .7)) {
                                        MenuItem m = mm.getActionItems().get(count - 1);
                                        _SetShowAsAction(m);
                                    }

                                }

                            }
                        });

            }
        }
    }

}