Lakmal K
Lakmal K

Reputation: 35

showAsAction=“ifRoom” doesn't behave as it should

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.

Upvotes: 3

Views: 1565

Answers (3)

Yuri F Becker
Yuri F Becker

Reputation: 368

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
    }

Upvotes: 2

Ashwin S Ashok
Ashwin S Ashok

Reputation: 3663

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>

Upvotes: 0

user3355820
user3355820

Reputation: 272

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"/>

Upvotes: 0

Related Questions