androidx.appcompat.view.menu.MenuBuilder Java Examples

The following examples show how to use androidx.appcompat.view.menu.MenuBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MBaseActivity.java    From HaoReader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 设置MENU图标颜色
 */
@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (menu != null) {
        if (menu instanceof MenuBuilder) {
            ((MenuBuilder) menu).setOptionalIconsVisible(true);
        }
        for (int i = 0; i < menu.size(); i++) {
            MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
            if (item.requiresOverflow()) {
                AppCompat.setTint(item, getResources().getColor(R.color.colorMenuText));
            } else {
                AppCompat.setTint(item, getResources().getColor(R.color.colorBarText));
            }
        }
    }
    return super.onCreateOptionsMenu(menu);
}
 
Example #2
Source File: LogcatActivity.java    From matlog with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("RestrictedApi")
public void invalidateDarkOrLightMenuItems(Context context, Menu menu) {
    if (menu instanceof MenuBuilder) {
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
        /*final boolean darkMode = ThemeUtils.isDarkMode(context);
        final int textColorPrimary = Utils.resolveColor(context, android.R.attr.textColorPrimary);

        mToolbar.post(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < menu.size(); i++) {
                    MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
                    int color = darkMode || item.isActionButton() ? Color.WHITE : textColorPrimary;
                    if (item.getIcon() != null) {
                        item.getIcon().setColorFilter(color, PorterDuff.Mode.SRC_IN);
                    }
                }
            }
        });*/
    }
}
 
Example #3
Source File: MainActivityTest.java    From meat-grinder with MIT License 5 votes vote down vote up
@UiThreadTest
@Test
public void testOnOptionsItemSelected() {
    @SuppressWarnings("RestrictedApi")
    MenuBuilder bld = new MenuBuilder(activity);
    @SuppressWarnings("RestrictedApi")
    MenuItem item = bld.add("test");
    activity.onOptionsItemSelected(item);
}
 
Example #4
Source File: OptionMenu.java    From WiFiAnalyzer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("RestrictedApi")
private void iconsVisible(Menu menu) {
    try {
        if (menu instanceof MenuBuilder) {
            ((MenuBuilder) menu).setOptionalIconsVisible(true);
        }
    } catch (Exception e) {
        // do nothing
    }
}
 
Example #5
Source File: Carbon.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public static Menu getMenu(Context context, int resId) {
    Context contextWrapper = context;
    Menu menu = new MenuBuilder(contextWrapper);
    MenuInflater inflater = new SupportMenuInflater(contextWrapper);
    inflater.inflate(resId, menu);
    return menu;
}
 
Example #6
Source File: DisplayUtils.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void setOverflowMenuIconsVisible(Menu menu) {
    if (menu == null) {
        return;
    }
    if (menu instanceof MenuBuilder) {
        try {
            Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
            method.setAccessible(true);
            method.invoke(menu, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example #7
Source File: BottomNavigationItemViewTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private MenuItemImpl createMenuItemImpl(CharSequence title, CharSequence tooltip) {
  MenuBuilder builder = new MenuBuilder(context);
  builder.add(title);
  MenuItemImpl menuItem = (MenuItemImpl) builder.getItem(0);
  menuItem.setTooltipText(tooltip);
  return menuItem;
}
 
Example #8
Source File: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void initForMenu(@NonNull Context context, @NonNull MenuBuilder menu) {
  layoutInflater = LayoutInflater.from(context);
  this.menu = menu;
  Resources res = context.getResources();
  paddingSeparator =
      res.getDimensionPixelOffset(R.dimen.design_navigation_separator_vertical_padding);
}
 
Example #9
Source File: ThemePreferencesManager.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("RestrictTo")
public void showChooseThemePopup(View anchor) {
  PopupMenu popupMenu = new PopupMenu(context, anchor);
  popupMenu.inflate(R.menu.mtrl_choose_theme_menu);
  if (popupMenu.getMenu() instanceof MenuBuilder) {
    MenuBuilder menuBuilder = (MenuBuilder) popupMenu.getMenu();

    menuBuilder.setOptionalIconsVisible(true);

    ColorStateList defaultColor =
        AppCompatResources.getColorStateList(
            context, R.color.material_on_surface_emphasis_medium);
    int selectedColor = MaterialColors.getColor(anchor, resourceProvider.getPrimaryColor());
    int currentThemeId = getCurrentThemeId();
    for (int i = 0; i < menuBuilder.size(); i++) {
      MenuItem item = menuBuilder.getItem(i);
      if (item.getItemId() == currentThemeId) {
        DrawableCompat.setTint(item.getIcon(), selectedColor);

        SpannableString s = new SpannableString(item.getTitle());
        s.setSpan(new ForegroundColorSpan(selectedColor), 0, s.length(), 0);
        item.setTitle(s);
      } else {
        DrawableCompat.setTintList(item.getIcon(), defaultColor);
      }
    }
  }
  popupMenu.setOnMenuItemClickListener(
      item -> {
        saveAndApplyTheme(item.getItemId());
        return false;
      });
  popupMenu.show();
}
 
Example #10
Source File: BottomNavigationMenuView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(MenuBuilder menu) {
  this.menu = menu;
}
 
Example #11
Source File: BottomNavigationPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
  return false;
}
 
Example #12
Source File: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
  if (callback != null) {
    callback.onCloseMenu(menu, allMenusAreClosing);
  }
}
 
Example #13
Source File: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
  return false;
}
 
Example #14
Source File: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
  return false;
}
 
Example #15
Source File: NavigationSubMenu.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onItemsChanged(boolean structureChanged) {
  super.onItemsChanged(structureChanged);
  ((MenuBuilder) getParentMenu()).onItemsChanged(structureChanged);
}
 
Example #16
Source File: NavigationMenuView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(MenuBuilder menu) {}
 
Example #17
Source File: BottomNavigationPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
  return false;
}
 
Example #18
Source File: BottomNavigationPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {}
 
Example #19
Source File: LoopBarView.java    From LoopBar with MIT License 4 votes vote down vote up
/**
 * Initiate LoopBar with menu
 *
 * @param menuRes id for inflating {@link Menu}
 */
public void setCategoriesAdapterFromMenu(@MenuRes int menuRes) {
    Menu menu = new MenuBuilder(getContext());
    new MenuInflater(getContext()).inflate(menuRes, menu);
    setCategoriesAdapterFromMenu(menu);
}
 
Example #20
Source File: BottomNavigationPresenter.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void initForMenu(Context context, MenuBuilder menu) {
  this.menu = menu;
  menuView.initialize(this.menu);
}
 
Example #21
Source File: MenuMainDemoFragment.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("RestrictTo")
private void showMenu(View v, @MenuRes int menuRes) {
  PopupMenu popup = new PopupMenu(getContext(), v);
  // Inflating the Popup using xml file
  popup.getMenuInflater().inflate(menuRes, popup.getMenu());
  // There is no public API to make icons show on menus.
  // IF you need the icons to show this works however it's discouraged to rely on library only
  // APIs since they might disappear in future versions.
  if (popup.getMenu() instanceof MenuBuilder) {
    MenuBuilder menuBuilder = (MenuBuilder) popup.getMenu();
    //noinspection RestrictedApi
    menuBuilder.setOptionalIconsVisible(true);
    for (MenuItem item : menuBuilder.getVisibleItems()) {
      int iconMarginPx =
          (int)
              TypedValue.applyDimension(
                  TypedValue.COMPLEX_UNIT_DIP, ICON_MARGIN, getResources().getDisplayMetrics());

      if (item.getIcon() != null) {
        if (VERSION.SDK_INT > VERSION_CODES.LOLLIPOP) {
          item.setIcon(new InsetDrawable(item.getIcon(), iconMarginPx, 0, iconMarginPx, 0));
        } else {
          item.setIcon(
              new InsetDrawable(item.getIcon(), iconMarginPx, 0, iconMarginPx, 0) {
                @Override
                public int getIntrinsicWidth() {
                  return getIntrinsicHeight() + iconMarginPx + iconMarginPx;
                }
              });
        }
      }
    }
  }
  popup.setOnMenuItemClickListener(
      menuItem -> {
        Snackbar.make(
                getActivity().findViewById(android.R.id.content),
                menuItem.getTitle(),
                Snackbar.LENGTH_LONG)
            .show();
        return true;
      });
  popup.show();
}