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();
}