3

I am developing an application for Android 4.0.3. I have multiple fragments and I have one menu item displayed on each of them. I want to have an overflow menu button that will have 2 menu items. How do I force the appearance of the overflow menu and add menu items to it ? I am adding menu items by calling the onCreateOptionsMenu function in each fragment.

Any and all help would be appreciated. Thanks!

4
  • What specifically is the problem that you are encountering? Commented Jun 1, 2012 at 13:51
  • I can't seem to figure out how to force the display of the overflow menu button. Commented Jun 1, 2012 at 13:52
  • 3
    Not all devices will show a button on-screen. Devices or emulators with a dedicated MENU button use that instead of an on-screen button, as that is what the users of those devices will be used to. Commented Jun 1, 2012 at 14:07
  • UPDATE: As of KitKat (4.4), devices will show an overflow button on screen whether or not they have a physical menu button. Of course, that doesn't help for earlier Android versions, but, FYI, it is at least a fun fact. Commented Mar 24, 2014 at 18:29

1 Answer 1

4

To force into the overflow menu do this:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   menu.add(0, 2, 0, "Item 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

   return true;
}

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW will force the item into either:

Android 3.0 + : The ActionBar as an overflow Android 2.3 - : The menu button

If you are wanting to recreate the menu you will need to call invalidateOptionsMenu(); This will re-create from OnCreateOptionsMenu.

Sign up to request clarification or add additional context in comments.

4 Comments

I have already tried that and it didn't work. An overflow menu button wasn't seen on the emulator.
I made an edit to my original question and I have added my code so you could see what I am exactly doing. After adding the above code you mentioned I still am not able to force the display of the overflow menu.
Commonsware is right. The emulator doesn't show the overflow button, but my tablet did. Thanks for the help!
You tablet probably shows the overflow button because it does not have a physical menu button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.