Java Code Examples for androidx.appcompat.widget.PopupMenu#getMenu()

The following examples show how to use androidx.appcompat.widget.PopupMenu#getMenu() . 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: ConfigActivity.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
private void showScreenPresets(View v) {
	PopupMenu popup = new PopupMenu(this, v);
	Menu menu = popup.getMenu();
	for (String preset : screenPresets) {
		menu.add(preset);
	}
	popup.setOnMenuItemClickListener(item -> {
		String string = item.getTitle().toString();
		int separator = string.indexOf(" x ");
		tfScreenWidth.setText(string.substring(0, separator));
		tfScreenHeight.setText(string.substring(separator + 3));
		return true;
	});
	popup.show();
}
 
Example 2
Source File: MainActivity.java    From AndPermission with Apache License 2.0 5 votes vote down vote up
/**
 * Create menu.
 */
private PopupMenu createMenu(View v, String[] menuArray) {
    PopupMenu popupMenu = new PopupMenu(this, v);
    Menu menu = popupMenu.getMenu();
    for (int i = 0; i < menuArray.length; i++) {
        String menuText = menuArray[i];
        menu.add(0, i, i, menuText);
    }
    return popupMenu;
}
 
Example 3
Source File: BottomSheetBuilder.java    From SimplicityBrowser with MIT License 5 votes vote down vote up
public BottomSheetBuilder setMenu(@MenuRes int menu) {
    @SuppressWarnings("ConstantConditions")
    PopupMenu popupMenu = new PopupMenu(mContext, null);
    mMenu = popupMenu.getMenu();
    popupMenu.getMenuInflater().inflate(menu, mMenu);
    return setMenu(mMenu);
}
 
Example 4
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 5
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();
}