3

I want to change the text size of popup menu item. I have written this code to create a popup menu. But my problem is I couldnot access the textView of popupmenu ie"EXIT" so that i could use

    exitItem.setTextSize(40);


    //my code
    popupmenu = new PopupMenu(MainActivity.this, findViewById(R.id.actionbar_item));
    popupmenu.getMenu().add(Menu.NONE, 0, Menu.NONE, "Exit");
    popupmenu.setOnMenuItemClickListener(this);

I have not created any seperate xml file to inflate these items. I want to do it programatically as these popupitems are dynamically generated.

Thanks in advance.

Paras Mittal
  • 1,139
  • 8
  • 18

2 Answers2

1

Unfortunately, it's not trivial. To change MenuItem appearance, even in this simple case, you'll have to set a custom View to it. But that will work only if MenuItem is shown as action, i.e. not in a list, which is not your case. To customise the appearance of your list in PopupMenu you have to use PopUpWindow instead. See this answer for example.

In your case, as you want to add elements dynamically maybe the best solution will be to implement your own Adapter with proper getView() method and use it together with ListView and PopUpWindow.

Community
  • 1
  • 1
Dmide
  • 6,183
  • 3
  • 21
  • 31
0

I'm afraid it cannot be done like that. The closest thing you can do is the following:

  • Create a View previousView which implements R.id.actionbar_item
  • Use that View to manipulate the text size
  • Add an item to your PopupMenu via popupmenu.getMenu().add(Menu.NONE, 0 , Menu.NONE, "Exit");
  • Create a MenuItem object menuItem via MenuItem menuItem = popupmenu.getMenu().getItem(0);
  • Then set menuItem.setActionView(previousView);

This is your best bet, but I haven't tested it yet. Good luck!

nstosic
  • 2,494
  • 1
  • 16
  • 21