Skip to content

Instantly share code, notes, and snippets.

@gotoark
Created February 27, 2017 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotoark/200ce866e64f9bf9d0f317dcb5370827 to your computer and use it in GitHub Desktop.
Save gotoark/200ce866e64f9bf9d0f317dcb5370827 to your computer and use it in GitHub Desktop.
Show Pop-Up Menu With Icon in Android
public void showPopupMenuWithIcon(View view) {
PopupMenu popup = new PopupMenu(SavingDatainSQLDB.this, view);
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();
}
popup.getMenuInflater().inflate(R.menu.badge_on_overflow_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment