0

Upon long press on any recycler view item, i want to show a popup. After seeing the Contacts app, i decided to use ContextMenu. But its setHeaderView function seems not working. setHeaderTitle function is working, again can't change its text colour. Menu items cannot have icons. But, all of these things are there in stock android, Contacts app. Any help is much appreciated.

@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
    contextMenu.setHeaderTitle(title);
    MenuInflater inflater = activity.getMenuInflater();
    inflater.inflate(R.menu.context_menu, contextMenu);
}

With PopupMenu this code works for icons, but not with ContextMenu.

try {
        // Reflection apis to enforce show icon
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(POPUP_CONSTANT)) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod(POPUP_FORCE_SHOW_ICON, boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
Jai
  • 217
  • 1
  • 4
  • 15
  • Check this : https://stackoverflow.com/questions/57290574/how-to-show-a-customized-popup-window-near-the-touch-location-like-what-we-have/57320757#57320757 – AskNilesh Sep 24 '19 at 11:00
  • 1
    @NileshRathod Why I didn't use popupwindow is to avoid the complexity of calculating the location and all. Right now the header textcolor is teal. If I could change that, am good. – Jai Sep 24 '19 at 11:10
  • @NileshRathod Not sure whether its a good approach, but i made use of Html.fromHtml() for the colour. – Jai Sep 24 '19 at 11:21

0 Answers0