Setting PopupMenu menu items programmatically
I have a PopupMenu
and I know the usual way to associate a menu to it is to use popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu());
or something of the like. My problem is, I have an array of items that I want in the menu and I need to be able to change them programmatically in Java. How can I do this?
Solution 1:
Just figured it out; for anyone who runs into this same problem you just do:
popup.getMenu().add(groupId, itemId, order, title);
for each MenuItem
you want to add.
Solution 2:
Just create the popup menu registering the view the popup will show underneath and use the getMenu() method to add the values. Don't forget to call show();
PopupMenu menu = new PopupMenu(this, view);
menu.getMenu().add("titleRes");
menu.show();