3

I created a basic project with "Minimum required SDK" API 11 to test Action Bar. Instead of showing action buttons on bar they ended up in action overflow, even the bar was empty. I used the following code.

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

But it started to show action buttons when I added the following namespace.

 xmlns:app="http://schemas.android.com/apk/res-auto"

and changed the android:showAsAction="ifRoom" to app:showAsAction="ifRoom".

As far as I know android:showAsAction="ifRoom" should work in API 11 and above.

Please help to clear this out.

3
  • 2
    If there's not enough room for the item in the action bar, it will appear in the action overflow. try android:showAsAction= "always" Commented Mar 17, 2014 at 5:27
  • There was plenty of space in the Action Bar as there was no action buttons at all. I tried "always" as well. But nothing happened. As I explained they appeared only after adding xmlns:app="http://schemas.android.com/apk/res-auto" namespace, which supposed to do for API 10 or less.
    – Lakmal K
    Commented Mar 17, 2014 at 5:36
  • Look at the min sdk in your manifest Commented Mar 17, 2014 at 6:00

3 Answers 3

2
xmlns:yourapp="http://schemas.android.com/apk/res-auto"

xmlns means xml namespace, it makes elements from the xml unique. You should always include this line do desambiguate two elements that share the same name.

More here: http://www.sitepoint.com/xml-namespaces-explained/

You could get rid of version incompability with this code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        //set action bar here
    }
0

you want to show the action search menu on action bar

then use

 <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

   <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always"/>
1
  • This is some thing else I guess
    – Lakmal K
    Commented Mar 17, 2014 at 5:42
0

Use this :

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_menu"
        android:icon="@drawable/menu_icon"
        android:title="@string/action_settings"
        yourapp:showAsAction="ifRoom"/>

</menu>
2
  • 1
    Thanks a lot. This is what I exactly did. Could you please explain the reason behind this?
    – Lakmal K
    Commented Mar 17, 2014 at 5:58
  • for appcompact you need to use it like this. Commented Mar 17, 2014 at 6:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.