In higher SDKs like 33 the PopupMenu with Icons is displayed incorrectly: Wrong PopupMenu with Icons
I don't know what is wrong.
I created it like this:
//init the wrapper with style
Context wrapper = new ContextThemeWrapper(anchor.getContext(), R.style.MyPopupStyle);
//init the popup
PopupMenu popup = new PopupMenu(wrapper, anchor);
/* The below code in try catch is responsible to display icons*/
if (isWithIcons) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
popup.setForceShowIcon(true);
} else {
try {
Field[] fields = popup.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
assert menuPopupHelper != null;
Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//inflate menu
popup.getMenuInflater().inflate(menu, popup.getMenu());
popup.show();
I tried with SDK 23 and there it is displayed correctly: Correct PopupMenu with Icons