Monday, 22 February 2016

How to keep Popup menu background white

If I use sample A the background of the overflow menu is white, just as expected. But if I use sample B then the background is black. I need sample B because I want the icons to show. But I need to keep the background white. How do I do that?

Sample A
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.menu_dogs, menu);
}

Sample B

findViewById(R.id.moreMenuBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
                popupMenu.inflate(R.menu.menu_dogs);
                forceShowIcon(popupMenu);
                popupMenu.show();
}
public static void forceShowIcon(PopupMenu popup) {
        try {
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 comment: