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:Main.java

/**
 * Android doesn't fade out disabled menu item icons, so do it ourselves
 *//*  w  w w  .ja  v a  2  s.c  o  m*/
public static void fixupMenuAlpha(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        Drawable icon = item.getIcon();
        if (icon != null) {
            icon.setAlpha(item.isEnabled() ? 255 : 64);
        }
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.MenuUtils.java

public static void tintAllMenuIcons(Menu menu, int color) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        tintMenuItemIcon(item, color);/*from ww  w. j  a  va2s. c  o m*/
    }
}

From source file:org.tasks.ui.MenuColorizer.java

/** Sets a color filter on all menu icons, including the overflow button (if it exists) */
private static void colorMenu(final Menu menu, final int color) {
    for (int i = 0, size = menu.size(); i < size; i++) {
        final MenuItem menuItem = menu.getItem(i);
        colorMenuItem(menuItem, color);/*w  ww .j  a  va  2s.  co  m*/
        if (menuItem.hasSubMenu()) {
            final SubMenu subMenu = menuItem.getSubMenu();
            for (int j = 0; j < subMenu.size(); j++) {
                colorMenuItem(subMenu.getItem(j), color);
            }
        }
    }
}

From source file:com.github.pennyfive.cinemafinlando.ui.activity.generic.DrawerActivity.java

private static void hideMenuItems(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(false);
    }/*from  www  .j av  a2 s.  co m*/
}

From source file:com.example.ray.firstapp.bottombar.MiscUtils.java

/**
 * A hacky method for inflating menus from xml resources to an array
 * of BottomBarTabs./*from   ww w. ja  v  a  2s .co m*/
 *
 * @param activity the activity context for retrieving the MenuInflater.
 * @param menuRes  the xml menu resource to inflate
 * @return an Array of BottomBarTabs.
 */
protected static BottomBarTab[] inflateMenuFromResource(Activity activity, @MenuRes int menuRes) {
    // A bit hacky, but hey hey what can I do
    PopupMenu popupMenu = new PopupMenu(activity, null);
    Menu menu = popupMenu.getMenu();
    activity.getMenuInflater().inflate(menuRes, menu);

    int menuSize = menu.size();
    BottomBarTab[] tabs = new BottomBarTab[menuSize];

    for (int i = 0; i < menuSize; i++) {
        MenuItem item = menu.getItem(i);
        BottomBarTab tab = new BottomBarTab(item.getIcon(), String.valueOf(item.getTitle()));
        tab.id = item.getItemId();
        tabs[i] = tab;
    }

    return tabs;
}

From source file:androidx.navigation.ui.NavigationUI.java

/**
 * Sets up a {@link NavigationView} for use with a {@link NavController}. This will call
 * {@link #onNavDestinationSelected(MenuItem, NavController)} when a menu item is selected.
 * The selected item in the NavigationView will automatically be updated when the destination
 * changes.//from  w  w  w.j ava  2  s  . c o m
 *
 * @param navigationView The NavigationView that should be kept in sync with changes to the
 *                       NavController.
 * @param navController The NavController that supplies the primary and secondary menu.
*                      Navigation actions on this NavController will be reflected in the
*                      selected item in the NavigationView.
 */
public static void setupWithNavController(@NonNull final NavigationView navigationView,
        @NonNull final NavController navController) {
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            boolean handled = onNavDestinationSelected(item, navController, true);
            if (handled) {
                ViewParent parent = navigationView.getParent();
                if (parent instanceof DrawerLayout) {
                    ((DrawerLayout) parent).closeDrawer(navigationView);
                }
            }
            return handled;
        }
    });
    navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
        @Override
        public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
            Menu menu = navigationView.getMenu();
            for (int h = 0, size = menu.size(); h < size; h++) {
                MenuItem item = menu.getItem(h);
                item.setChecked(matchDestination(destination, item.getItemId()));
            }
        }
    });
}

From source file:androidx.navigation.ui.NavigationUI.java

/**
 * Sets up a {@link BottomNavigationView} for use with a {@link NavController}. This will call
 * {@link #onNavDestinationSelected(MenuItem, NavController)} when a menu item is selected. The
 * selected item in the BottomNavigationView will automatically be updated when the destination
 * changes./*from  w  w  w. j  a v a2s.  co m*/
 *
 * @param bottomNavigationView The BottomNavigationView that should be kept in sync with
 *                             changes to the NavController.
 * @param navController The NavController that supplies the primary menu.
*                      Navigation actions on this NavController will be reflected in the
*                      selected item in the BottomNavigationView.
 */
public static void setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView,
        @NonNull final NavController navController) {
    bottomNavigationView
            .setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    return onNavDestinationSelected(item, navController, true);
                }
            });
    navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
        @Override
        public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
            Menu menu = bottomNavigationView.getMenu();
            for (int h = 0, size = menu.size(); h < size; h++) {
                MenuItem item = menu.getItem(h);
                if (matchDestination(destination, item.getItemId())) {
                    item.setChecked(true);
                }
            }
        }
    });
}

From source file:me.henrytao.mdcore.core.MdCompat.java

public static void supportDrawableTint(Context context, Menu menu, Palette palette) {
    if (menu != null) {
        int i = 0;
        for (int n = menu.size(); i < n; i++) {
            supportDrawableTint(context, menu.getItem(i), palette);
        }/*from  w  ww  . j a  v  a 2 s  .com*/
    }
}

From source file:com.silentcircle.common.util.ViewUtil.java

public static void tintMenuIcons(@NonNull Menu menu, final int color) {
    for (int i = 0; i < menu.size(); ++i) {
        final MenuItem item = menu.getItem(i);
        Drawable icon = item.getIcon();/*from  w w w. j a v  a  2  s . c o  m*/
        if (icon != null) {
            icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
            item.setIcon(icon);
        }
    }
}

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./*  www .  ja  va2 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));
        }
    };

}