4

I am building the menu items of my Actionbar programmatically, due to a bug in Android when having two Fragments each with their own menu.

I am building the menu in onCreateActionMode of the MultiChoiceModeListener. If there are more than 4 menu items all of the items are forced into the overflow menu of the contextual action bar.

This happens in portait and landscape mode, but only when the layout is in the non-tablet view (eg, both fragments are not displayed). I'm testing on a Galaxy Nexus with Android 4.2

    mListView = getListView();
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

    mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) {
            // Inflate the menu for the CAB
            menu.add(1, 0, 0, "Menu Item 1");
            menu.add(1, 1, 1, "Menu Item 2");
            menu.add(1, 2, 2, "Menu Item 3");
            menu.add(1, 3, 3, "Menu Item 4");
            menu.add(1, 4, 4, "Menu Item 5");
            return true;
        }
    }

UPDATE

I've narrowed down this issue to if there are more than 4 menu items all items are force into overflow. The length of the text label doesn't matter. I even set all the text to empty strings and they were still forced into overflow. Setting IF_ROOM or ALWAYS doesn't seem to have an effect.

UPDATE 2

I have another Fragment that is using a OnItemLongClickListener, instead of a MultiChoiceModeListener, and and I'm not experiencing the same issue. I can put in 10 menu items and it will display the first couple in the Actionbar and put the rest into overflow, as expected.

UPDATE 3

A little more info on this issue, with a Nexus 7 in portrait mode the issue still exists, however, in the non-tablet view (both fragments are not displayed), in landscape mode on the Nexus 7, the menu items display. The only thing I can think of is there is some spacing calculation going on when using a MultiChoiceModeListener that thinks there is no room in the contextual actionbar when there is.

Kris B
  • 3,436
  • 9
  • 64
  • 106

1 Answers1

1

The Menu.add() methods all return MenuItems. For each MenuItem added, just call MenuItem.setShowAsAction(int) like this:

MenuItem menuItem = menu.add(1, 0, 0, "Menu Item 1");
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
ajpolt
  • 1,002
  • 6
  • 10
  • I shortened it to: `menu.add(1, 0, 0, "Menu Item 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);` but still didn't fix the overflow issue, though I can now add icons to my menu items using `setIcon` which I didn't know before. – Kris B Nov 20 '12 at 20:11
  • 2
    You should use `IF_ROOM` over `ALWAYS` in almost all cases to ensure you don't force too many items onto the screen. – Jake Wharton Nov 20 '12 at 23:51
  • I updated all of them to `IF_ROOM`. I've narrowed down this issue to if there are more than 4 menu items, in portrait mode, all items are force into overflow. Setting `IF_ROOM` or `ALWAYS` doesn't seem to have an effect. I even set all the text labels to empty strings and they were still forced into overflow. Very strange. – Kris B Nov 22 '12 at 20:56
  • @JakeWharton I've updated my OP with some more details. I'm pretty much at a loss and just thinking of not spending anymore time debugging this. It's not a deal breaker, as the menu items work, they just are forced into overflow. Just a strange issue, is all. – Kris B Nov 26 '12 at 17:52