Example usage for android.view Menu findItem

List of usage examples for android.view Menu findItem

Introduction

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

Prototype

public MenuItem findItem(int id);

Source Link

Document

Return the menu item with a particular identifier.

Usage

From source file:Main.java

public static void setMenuItemVisible(final Menu menu, final boolean visible, final int viewId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setVisible(visible);//  w  w  w  . j  a  v  a2 s . c o m
    }
}

From source file:Main.java

public static void setMenuItemChecked(final Menu menu, final boolean checked, final int viewId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setChecked(checked);/*from w w w  . j a v a2  s  .  c o  m*/
    }
}

From source file:Main.java

public static void setMenuItemEnabled(final Menu menu, final boolean enabled, final int viewId,
        final int enabledResId, final int disabledResId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setIcon(enabled ? enabledResId : disabledResId);
        v.setEnabled(enabled);//w w w.  j a  v  a  2 s . co  m
    }
}

From source file:Main.java

public static void setMenuItemChecked(final Menu menu, final boolean checked, final int viewId,
        final int checkedResId, final int uncheckedResId) {
    final MenuItem v = menu.findItem(viewId);
    if (v != null) {
        v.setChecked(checked);/* w ww  .j  ava  2  s  . c  o m*/
        v.setIcon(checked ? checkedResId : uncheckedResId);
    }
}

From source file:com.afzaln.myweatherapp.custom.action.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.//  w  w  w.ja  va 2s. c o  m
 *
 * <p>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {

    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder(
                    "Menu item was not found, " + "available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class),
                    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };

}

From source file:it.scoppelletti.mobilepower.app.CommonMenuFragment.java

/**
 * Imposta la visibilit&agrave; di tutte le voci comuni del men&ugrave;.
 * /*from   ww  w .  j  a v  a 2s . com*/
 * @param menu    Men&ugrave;.
 * @param visible Indicatore di visibilit&agrave;.
 */
public static void setMenuItemVisible(Menu menu, boolean visible) {
    MenuItem menuItem;

    menuItem = menu.findItem(R.id.cmd_buy);
    if (menuItem != null) {
        menuItem.setVisible(visible);
    }

    menuItem = menu.findItem(R.id.cmd_settings);
    menuItem.setVisible(visible);
    menuItem = menu.findItem(R.id.cmd_help);
    menuItem.setVisible(visible);
    menuItem = menu.findItem(R.id.cmd_about);
    menuItem.setVisible(visible);
}

From source file:com.albedinsky.android.ui.widget.ActionTextButton.java

/**
 * Obtains action view of an item with the specified <var>itemId</var> from the given <var>menu</var>.
 *
 * @param menu   The menu where is the item presented.
 * @param itemId Id of the menu item of which action view to obtain.
 * @return Obtained view or {@code null} if the item does not have action view or it is not
 * presented within the given menu.//  ww  w. j av a 2 s. c  o m
 */
@SuppressLint("NewApi")
private static View obtainActionView(Menu menu, int itemId) {
    final MenuItem menuItem = menu.findItem(itemId);
    if (menuItem == null)
        return null;
    return COMPAT_MODE ? MenuItemCompat.getActionView(menuItem) : menuItem.getActionView();
}

From source file:com.android.contacts.group.GroupMembersFragment.java

private static void setVisible(Context context, Menu menu, int id, boolean visible) {
    final MenuItem menuItem = menu.findItem(id);
    if (menuItem != null) {
        menuItem.setVisible(visible);//from  w  w w  .j av a  2 s  .  c o  m
        final Drawable icon = menuItem.getIcon();
        if (icon != null) {
            icon.mutate().setColorFilter(ContextCompat.getColor(context, R.color.actionbar_icon_color),
                    PorterDuff.Mode.SRC_ATOP);
        }
    }
}

From source file:MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.findItem(MENU_DOWNLOAD);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menuItem.setVisible(showDownloadMenu);
    return true;//from  w  w  w.jav  a 2  s . co  m
}

From source file:com.android.deskclock.actionbarmenu.SearchMenuItemController.java

@Override
public void showMenuItem(Menu menu) {
    menu.findItem(SEARCH_MENU_RES_ID).setVisible(true);
}