Oualid Bencharki
Oualid Bencharki

Reputation: 47

How to keep popup menu open when clicking on one of its items?

I have a button in actionbar ,when I click on it it display a popup menu with items , I want to prevent the popup from closing when clicking on an item ; here is the code I use for the popup :

 PopupMenu popup = new PopupMenu(this, popupButton);
 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.menu_story, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {            
    //code             
    return true ;
});

Upvotes: 0

Views: 1040

Answers (1)

Streletz
Streletz

Reputation: 202

If you don't want popupmenu to close immediately after a click, You need to create your own pop-up window instead of the standard popupmenu. For example, based on AlertDialog.

I will not give an example of the code, because it is in the documentation.

Upvotes: 0

Related Questions