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:com.alainesp.fan.sanderson.MainActivity.java

/**
 * Navigate to a given menu index/*  w  ww .  j a  v  a2 s.  c  o  m*/
 * @param appState The index
 */
public static void navigateTo(int appState) {
    Menu drawerMenu = navigationView.getMenu();
    MenuItem selectItem;

    //        if(appState >= APP_STATE_WOK && appState <= APP_STATE_WOR)
    //        {
    //            selectItem = drawerMenu.getItem(APP_STATE_WOK).getSubMenu().getItem(appState-APP_STATE_WOK);
    //        }
    //        else
    selectItem = drawerMenu.getItem(appState);

    staticRef.onNavigationItemSelected(selectItem);
}

From source file:com.nextgis.maplibui.fragment.BottomToolbar.java

@Override
public void inflateMenu(@MenuRes int resId) {
    super.inflateMenu(resId);
    Menu menu = getMenu();
    MenuItem item = menu.getItem(0);
    int size = item.getIcon().getIntrinsicWidth() + ControlHelper.dpToPx(30, getResources());
    int width = getWidth();

    for (int i = 0; i < menu.size(); i++) {
        item = menu.getItem(i);//ww  w. j  av a 2  s.c  om
        if (size * (i + 2) < width)
            MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
        else
            break;
    }
}

From source file:io.github.hidroh.materialistic.MenuTintDelegate.java

/**
 * Callback that should be triggered after menu has been inflated
 * @param menu    inflated menu/*from  w  w w .j a  va2  s  . c o  m*/
 */
public void onOptionsMenuCreated(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        Drawable drawable = menu.getItem(i).getIcon();
        if (drawable == null) {
            continue;
        }
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, mTextColorPrimary);
    }
}

From source file:com.example.android.hcgallery.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.getItem(0).setTitle(mToggleLabels[mLabelIndex]);
    return true;
}

From source file:com.qasp.diego.arsp.SettingsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.meumenu, menu);
    menu.getItem(0).setVisible(false);
    return true;// w ww  .j a v a  2 s  . co  m
}

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./*from w  w w  .j a v a2  s.c om*/
 *
 * <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:com.justplay1.shoppist.utils.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.//  w w  w  .  j  a  v  a  2s .co  m
 * <p>
 * <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 (Resources.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:org.melky.geekjuniorapp.LicenseGeekFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (menu.hasVisibleItems())
        menu.removeItem(menu.getItem(0).getItemId());

}

From source file:com.mifos.mifosxdroid.tests.action.NavigationViewActions.java

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.//w ww .j av a 2 s.  co m
 * <p/>
 * <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(20)
                    .append("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:com.vrem.wifianalyzer.navigation.NavigationMenuView.java

public void setCurrentNavigationMenu(@NonNull NavigationMenu navigationMenu) {
    this.currentNavigationMenu = navigationMenu;
    Menu menu = navigationView.getMenu();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        item.setCheckable(navigationMenu.ordinal() == i);
        item.setChecked(navigationMenu.ordinal() == i);
    }// w w  w  .j ava2s . c  o  m
}