24

i have a very simple code, but a problem that I cannot solve even after long google searching. I want to have some Action Items in my ActionBar, but whenever I run the App, all I see is a ActionBar with the App logo and the title, but no Action Items.

It would be great, if you could help me, probably I am just missing the most obvious thing ;)

Thats the method in my ActionBarActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

And this is the relevant .xml file for the ActionBar (named main_activity_actions.xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always"  />
<item android:id="@+id/action_compose"
      android:icon="@drawable/ic_action_compose"
      android:title="@string/action_compose" 
      android:showAsAction="always"/>
</menu>
Kilian Obermeier
  • 6,678
  • 4
  • 38
  • 50
  • The code appears correct. But which is your minTargetSdk declared in your manifest file, and what is the version of android where you are testing the app?? – lory105 Jul 28 '13 at 21:45

4 Answers4

69

This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should create your menus in a different than the standard way of creating XML menus in ActioBarSherlock or the default ActionBar.

So try this code:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          app:showAsAction="always"  />
      <item android:id="@+id/action_compose"
          android:icon="@drawable/ic_action_compose"
          android:title="@string/action_compose" 
          app:showAsAction="always"/>
</menu>

and report if this works.

Note: check the extra prefix xmlns:app which should be used instead!

halfer
  • 19,824
  • 17
  • 99
  • 186
Manolis Proimakis
  • 1,787
  • 16
  • 22
  • 5
    so to clarify, "app" is used 3 times: in menu and once in each item. Had to apply a diff to catch that :-) – U Avalos Sep 07 '13 at 02:53
  • 1
    it has to do with using the app compat library so you can use this with older versions of android. If you are targeting new version then you don't need to do this. and set your api min higher. – reidisaki Sep 23 '14 at 18:34
6

May sure that you are not using a Style that is rendering your Action Items invisible.

For example if you are using "android:Theme.Holo.Light.DarkActionBar" you get a black action bar so you won't be able to see your black items.

Switch to "android:Theme.Holo.Light" and they will show up.

Look in the file (for example) res/values-v14/styles.xml

1

Here you go, you need to add the menu in your xml like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="0"
        android:showAsAction="always"
        android:icon="@drawable/menu">
    <menu>
        <item android:id="@+id/action_search"
            android:icon="@drawable/ic_action_search"
            android:title="@string/action_search"
            android:showAsAction="always"  />
        <item android:id="@+id/action_compose"
            android:icon="@drawable/ic_action_compose"
            android:title="@string/action_compose" 
            android:showAsAction="always"/>
        </menu>
    </item>
</menu>
Marcel50506
  • 1,280
  • 1
  • 14
  • 32
Peshal
  • 1,508
  • 1
  • 12
  • 22
0

Try this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return true;
}
Steve Benett
  • 12,843
  • 7
  • 59
  • 79