Search code examples
androidandroid-popupwindow

Customize the text in a popup menu item programmatically


I have newly implemented popup menus in my Android project, but I would like to programmatically change the text for one of the menu items, depending on software state. Is this possible with a pop up menu item?? I can already do it with my Action Bar items using menu.findItem(), but that function doesn't seem to be provided for popupmenu.

This is my code for displaying the menu:

private void showPopupMenu (final View v)
{
    PopupMenu pm = new PopupMenu (net, v);
    pm.getMenuInflater().inflate (R.menu.popupmenu, pm.getMenu());

    pm.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener()
    {
            ...

I suppose I could create a big set of popupmenu XML's and parse the one I need in inflate(), but really doesn't seem like an efficient way to do it.


Solution

  • Ultimately, what I decided to do was this:

        pm.getMenuInflater().inflate (bConfigSelf
            ?   R.menu.SelfConfigMenu   //  Need a different menu for myself
            :   R.menu.ConfigMenu,      //   than for a neighbor
                    pm.getMenu());
    

    A choice of 2 different menus didn't seem too outlandish, but this isn't really a solution for the general case. But I'm going to assume (based on the number of responses) that this isn't a common problem, and that there really isn't a solution to it at this point.