16

I am following the tutorial on developer.android.com and trying to add items on action bar.

Although i added all the code the search action shows as an overflow element instead of an action button element. I tried on 4" and 7" virtual devices with soft keyboard option.

Here is the

main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never" />
</menu>

Here is the MainActivity.java 's onCreateOptionsMenu method.

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

enter image description here

I want to learn what causes this problem.

Salih Erikci
  • 5,076
  • 12
  • 39
  • 69

2 Answers2

48

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>
Muthuraja
  • 551
  • 5
  • 13
  • 4
    For anyone else with this same problem the 'xmlns' namespaces at the top of the xml definition are important. – MarcF May 28 '14 at 19:03
  • @MarcF - why those are important ?? – Anjum Dec 11 '14 at 11:03
  • @Anjum - As in it wouldn't work for me without those declarations. Perhaps obvious to someone else but at the time it wasn't to me. – MarcF Dec 11 '14 at 11:46
  • Thanks, this line [xmlns:app="http://schemas.android.com/apk/res-auto"] works for me. – Anjum Dec 11 '14 at 11:47
  • @Anjum The namespace declaration must be written in order to achieve Support Library compatibility which is used for older API levels. Read more here: [Support Library](http://developer.android.com/tools/support-library/index.html) – coder101 Mar 04 '15 at 05:58
  • @Muthuraja G : Your knowledge just saved my day... Thanks a ton !!! – AndoAiron Apr 17 '15 at 01:23
  • For me it just display 2 icons =( i need more, even doing all this "tricks" – user2582318 Jun 29 '15 at 13:53
  • @Muthuraja, i have the same code but the action button isn't showing in the actionbar?? – alex Jul 24 '15 at 12:47
8

Though the issue is resolved, let me post an answer with more information, may be found helpful by anyone later.

Now, the issue is you have used android:showAsAction="ifRoom" and android:showAsAction="never", instead if you would want to make action button always visible then use android:showAsAction="always"

FYI, android:showAsAction can take either of any values:

android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

enter image description here

You can read more about Menu Resource

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295