Configuring PopupMenu menu items programmatically

I have PopupMenu , and I know that the usual way to associate a menu with it is to use popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu()); or something similar. My problem is that I have a set of elements that I want in the menu, and I need to be able to change them programmatically in Java. How can i do this?

Thank!

+43
android android-menu
Dec 09 '12 at 2:28
source share
6 answers

Just figured it out; for those facing the same problem you are just doing:

 popup.getMenu().add(groupId, itemId, order, title); 

for every MenuItem you want to add.

+91
Dec 09
source share

Just create a popup menu that registers the view that the popup will display below, and use the getMenu () method to add values. Remember to call show ();

 PopupMenu menu = new PopupMenu(this, view); menu.getMenu().add("titleRes"); menu.show(); 
+23
Jan 12 '13 at 9:11
source share

Defines identifiers in popupmenu.xml

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/slot1" app:showAsAction="ifRoom|withText" android:title="Movies" android:visible="true"/> <item android:id="@+id/slot2" app:showAsAction="ifRoom|withText" android:title="Music" android:visible="true"/> <item android:id="@+id/slot3" app:showAsAction="ifRoom|withText" android:title="Comedy" android:visible="true"/> </menu> 

 PopupMenu popupMenu = new PopupMenu(FullMenuActivity.this, view); popupMenu.setOnMenuItemClickListener(FullMenuActivity.this); popupMenu.getMenu().add(1, R.id.slot1, 1, "slot1"); popupMenu.getMenu().add(1,R.id.slot2,2,"slot2"); popupMenu.getMenu().add(1,R.id.slot3,3,"slot3"); popupMenu.show(); 

  @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.slot1: SessionManager.selected_slot = item.getTitle().toString(); Toast.makeText(this, "slot1 Clicked", Toast.LENGTH_SHORT).show(); return true; case R.id.slot2: SessionManager.selected_slot = item.getTitle().toString(); Toast.makeText(this, "slot2 Clicked", Toast.LENGTH_SHORT).show(); return true; case R.id.slot3: SessionManager.selected_slot = item.getTitle().toString(); Toast.makeText(this, "slot3 Clicked", Toast.LENGTH_SHORT).show(); return true; default: return true; } } 
+11
Dec 28 '15 at 14:24
source share

Yes.! it will help you. Try the following :)

Dynamic_PopUpMenu.setOnClickListener (new View.OnClickListener () {

  @Override public void onClick(View v) { PopupMenu menu = new PopupMenu(DialogCheckBox.this, v); menu.getMenu().add("AGIL"); menu.getMenu().add("Dash"); menu.getMenu().add("AGILarasan"); menu.getMenu().add("Arasan"); menu.show(); } }); 
+10
Dec 07 '16 at 4:39
source share

@ Vura Tarun gave a good answer, and I based my answer on this: First, instead of using fake mune.xml resources mune.xml I believe that the best approach is to create the ids file:

ids.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <item name="menuGroup" type="id"/> <item name="menu1" type="id"/> <item name="menu2" type="id"/> <item name="menu3" type="id"/> </resources> 

Then you can do something like this:

 private void showPopup(final View anchor) { PopupMenu popupMenu = new PopupMenu(anchor.getContext(), anchor); popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, Menu.NONE, "slot1"); popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, Menu.NONE,"slot2"); popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, Menu.NONE,"slot3"); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Toast.makeText(anchor.getContext(), item.getTitle() + "clicked", Toast.LENGTH_SHORT).show(); return true; } }); popupMenu.show(); } 
+2
Sep 06 '16 at 8:16
source share

Here is a complete solution with set and processed identifiers:

  this.findViewById(R.id.hamburger_menu).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu menu = new PopupMenu(getApplicationContext(), v); menu.getMenu().add(Menu.NONE, 1, 1, "Share"); menu.getMenu().add(Menu.NONE, 2, 2, "Comment"); menu.show(); menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int i = item.getItemId(); if (i == 1) { //handle share return true; } else if (i == 2) { //handle comment return true; } else { return false; } } }); } }); 

Note: a shared resource and comments, for example, you can also put constants for numbers 1,2 to make the code more readable.

In addition, I put Menu.NONE because I do not need to group menu items. If you want to create a group group group id = 1, 2, etc.

+1
Sep 18 '16 at 15:40
source share



All Articles