5

If I define the following items for my action bar:

res/menu/action_menu.xml :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="label"/>
    <item android:title="label1"/>
    <item android:title="label2"/>
    <item android:title="label3"/>
    <item android:title="label4"/>

</menu>

In my Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_menu, menu);
    return true;
}

Is there anyway to allow me define certain items move to action overflow part ? and how to do it?

P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • 3
    the way you declared all items will be on the overflow, you need to declare with setAsAction to show as an action – Marcio Covre Feb 14 '12 at 11:27

2 Answers2

9

It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags

E.g.

<item android:id="@+id/refresh"
      android:title="@string/refresh"
      android:icon="@drawable/reload_button"
      android:showAsAction="always"/>

Here android:showAsAction tells how to handle it. Options are

  • always
  • ifRoom
  • never
  • withText

You can or options together with the pipe symbol as "always|withText"

See the android docs for action bar for more documentation.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    Great answer. It really helped me understand how the Action Bar is meant to be! I do have one question though. I want certain things to only go in the Action Overflow part, meaning, it is under the 3 dot spinner image that Android puts on the top right. How can I force it to do that? – Andy Jul 23 '12 at 04:46
  • 4
    Just to clarify, because it was pretty hard to find this spelled out anywhere, if you set showAsAction to "never" it will be moved to the overflow menu. (The docs talk in many places about relegating non-frequently used actions to the overflow, but nowhere with any clues for how to make that happen... Even if you suspect showAsAction, the official docs just say "never: Never place this item in the Action Bar."--they do not make it clear where it goes instead if anywhere. Also note on some devices the "overflow" may end up under the physical Menu button instead of the action bar.) – Brandyn Aug 16 '13 at 00:46
7

To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.

On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".

nsemeniuk
  • 1,171
  • 1
  • 13
  • 24
  • 3
    "To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed." -- or if you set as "never" regardless of whether there is room. – Brandyn Aug 16 '13 at 00:48