Kris B
Kris B

Reputation: 3578

Contextual action bar forces overflow

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.

Upvotes: 4

Views: 882

Answers (1)

ajpolt
ajpolt

Reputation: 1002

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);

Upvotes: 1

Related Questions