1

I'm trying to understand what is wrong with what I'm doing. I'm currently following the android action bar tutorial and for some reason it's not showing me the search icon in the action bar on my device, it is going straight into my overflow. I'm running 4.4.4 version on my device, Nexus 5. I've tried to follow several tutorials and the result was no different. I've set up new project by default using API 11 as told, I've tried to set the showAsAction to "Always". What else could I do? Thank you for any help!

My XML menu file:

<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_new"
      android:title="new"
      android:showAsAction="ifRoom" />

<item android:id="@+id/action_settings"
      android:title="Settings"
      android:showAsAction="never" />

Main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

1 Answers1

1

Try using a namespace

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:{anynamespace}="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_new"
      android:title="new"
      {anynamespace}:showAsAction="always" />
</menu>

Replace the {anynamespace} parts with any word

EDIT: The tutorial suggests that you use namespaces in case you are using the Support library.

Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • Can you explain? Or at least direct me to any link on the subject that I could read? And thank you a lot! It did worked, although I did tried it before and it didn't. I've must missed something last time. – Evgeny Goldin Nov 17 '14 at 19:43
  • glad i could help :) I updated the answer, the link in the answer redirects to the same tutorial you added, Just note that this is only used in case of support library – Abdallah Alaraby Nov 17 '14 at 19:51
  • Yeah, I didn't read it because the sentence began with the words "for compatibility on versions as low as Android 2.1", so I skipped it. So how come I did had to use the name space? I did create the app with minimum required sdk set to 11. Why did I had to use this in my case? Any thoughts? – Evgeny Goldin Nov 17 '14 at 20:05
  • I think it's because you're using ActionBarActivity which is part of the v7 support library. As once you're using the support library, it doesn't matter which API level is your minimum, as long as it isn't less than the support library's minimum API level. – Abdallah Alaraby Nov 17 '14 at 20:28
  • Were I suppose not to use ActionBarActivity? Thank you again for all your help by the way! – Evgeny Goldin Nov 17 '14 at 20:36
  • It depends on the purpose of the Activity you want to implement, see [this answer](http://stackoverflow.com/a/21514470/2185634) for more info. you're most welcome :) – Abdallah Alaraby Nov 17 '14 at 20:42