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.justplay1.shoppist.utils.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  a 2 s  . c om
 * <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: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.//from   w w w  . j  ava2s  . com
 * <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:io.github.hidroh.materialistic.MenuTintDelegate.java

/**
 * Callback that should be triggered after menu has been inflated
 * @param menu    inflated menu//from   w  w  w.ja v a2 s . co 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.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);
    }/*from www  .j av a 2 s . c o  m*/
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void wrapMenuIcon(@NonNull Menu menu, int itemColor, int subItemColor, int... excludeGroups) {
    for (int i = 0, j = menu.size(); i < j; i++) {
        final MenuItem item = menu.getItem(i);
        wrapMenuItemIcon(item, itemColor, excludeGroups);
        if (item.hasSubMenu()) {
            wrapMenuIcon(item.getSubMenu(), subItemColor, subItemColor, excludeGroups);
        }//from  w  ww .  j a v  a 2 s. co m
    }
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void wrapMenuIcon(ActionMenuView view, int... excludeGroups) {
    final int itemBackgroundColor = ThemeUtils.getThemeBackgroundColor(view.getContext());
    final int popupItemBackgroundColor = ThemeUtils.getThemeBackgroundColor(view.getContext(),
            view.getPopupTheme());//from   w w w .  j  a  va  2s.c  o m
    final Resources resources = view.getResources();
    final int colorDark = resources.getColor(R.color.action_icon_dark);
    final int colorLight = resources.getColor(R.color.action_icon_light);
    final int itemColor = ColorUtils.getContrastYIQ(itemBackgroundColor, colorDark, colorLight);
    final int popupItemColor = ColorUtils.getContrastYIQ(popupItemBackgroundColor, colorDark, colorLight);
    final Menu menu = view.getMenu();
    final int childCount = view.getChildCount();
    for (int i = 0, j = menu.size(), k = 0; i < j; i++) {
        final MenuItem item = menu.getItem(i);
        wrapMenuItemIcon(item, itemColor, excludeGroups);
        if (item.hasSubMenu()) {
            wrapMenuIcon(menu, popupItemColor, popupItemColor, excludeGroups);
        }
        if (item.isVisible()) {
            k++;
        }
    }

}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem_Star = menu.add(0, MENU_ITEM_STAR, menu.size(), R.string.description_star);
    MenuItemCompat.setShowAsAction(menuItem_Star, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);

    if (mIsStarred) {
        menu.getItem(MENU_ITEM_STAR).setIcon(R.drawable.ic_menu_star_on);
    } else {/* ww w.  j  ava2s.  c  om*/
        menu.getItem(MENU_ITEM_STAR).setIcon(R.drawable.ic_menu_star);
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:org.getlantern.firetweet.util.ThemeUtils.java

public static void wrapMenuIcon(ActionMenuView view, int... excludeGroups) {
    final int itemBackgroundColor = ThemeUtils.getThemeBackgroundColor(view.getContext());
    final int popupItemBackgroundColor = ThemeUtils.getThemeBackgroundColor(view.getContext(),
            view.getPopupTheme());/* ww w . j  a v a 2s.c  o  m*/
    final Resources resources = view.getResources();
    final int colorDark = resources.getColor(R.color.action_icon_dark);
    final int colorLight = resources.getColor(R.color.action_icon_light);
    final int itemColor = ColorUtils.getContrastYIQ(itemBackgroundColor, colorDark, colorLight);
    final int popupItemColor = ColorUtils.getContrastYIQ(popupItemBackgroundColor, colorDark, colorLight);
    final Menu menu = view.getMenu();
    final int childCount = view.getChildCount();
    for (int i = 0, j = menu.size(), k = 0; i < j; i++) {
        final MenuItem item = menu.getItem(i);
        wrapMenuItemIcon(item, itemColor, excludeGroups);
        if (item.hasSubMenu()) {
            wrapMenuIcon(menu, popupItemColor, popupItemColor, excludeGroups);
        }
        if (item.isVisible()) {
            k++;
        }
    }
}

From source file:com.app.blockydemo.ui.fragment.FormulaEditorListFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    for (int index = 0; index < menu.size(); index++) {
        menu.getItem(index).setVisible(false);
    }//from   ww  w . j av a  2  s .co  m

    getActivity().getActionBar().setDisplayShowTitleEnabled(true);
    getActivity().getActionBar().setTitle(actionBarTitle);
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);

    super.onPrepareOptionsMenu(menu);
}

From source file:com.alibaba.weex.extend.module.WXTitleBar.java

@JSMethod
public void setStyle(JSONObject object) {
    String bgColor = object.getString("backgroundColor");
    String color = object.getString("foregroundColor");
    ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        if (bgColor != null) {
            int c = WXResourceUtils.getColor(bgColor);
            actionBar.setBackgroundDrawable(new ColorDrawable(c));
        }//from ww  w.  ja v a 2s .  c  o m

        if (color != null) {
            int c = WXResourceUtils.getColor(color);

            Toolbar toolbar = (Toolbar) ((Activity) mWXSDKInstance.getContext()).findViewById(R.id.toolbar);
            if (toolbar != null) {
                toolbar.setTitleTextColor(c);
                toolbar.setSubtitleTextColor(c);

                Drawable upNavigation = toolbar.getNavigationIcon();
                if (null != upNavigation) {
                    upNavigation = DrawableCompat.wrap(upNavigation);
                    upNavigation = upNavigation.mutate();
                    DrawableCompat.setTint(upNavigation, c);
                    toolbar.setNavigationIcon(upNavigation);
                }

                Drawable overflowIcon = toolbar.getOverflowIcon();
                if (null != overflowIcon) {
                    overflowIcon = DrawableCompat.wrap(overflowIcon);
                    overflowIcon = overflowIcon.mutate();
                    DrawableCompat.setTint(overflowIcon, c);
                    toolbar.setOverflowIcon(overflowIcon);
                }

                Menu menu = toolbar.getMenu();
                if (menu != null && menu.size() > 0) {
                    for (int i = 0; i < menu.size(); i++) {
                        MenuItem item = menu.getItem(i);
                        if (item != null && item.getIcon() != null) {
                            Drawable drawable = item.getIcon();
                            if (null != drawable) {
                                drawable = DrawableCompat.wrap(drawable);
                                drawable = drawable.mutate();
                                DrawableCompat.setTint(drawable, c);
                                item.setIcon(drawable);
                            }
                        }
                    }
                    ((Activity) mWXSDKInstance.getContext()).invalidateOptionsMenu();
                }
            }
        }
    }
}