Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app:showAsAction ifRoom is not working on appcompat action bar

Tags:

android

I have an action bar with the following menu items;

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.blah.blah.app.ClientActivity" >

    <item android:id="@+id/action_search"
        android:icon="@drawable/search"
        android:title="@string/action_search"
        android:orderInCategory="1"
        app:showAsAction="ifRoom|withText"/>

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="5"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_waiter"
        android:title="@string/action_waiter"
        android:orderInCategory="6"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_cleantable"
        android:title="@string/action_cleantable"
        android:orderInCategory="7"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_suggest"
        android:title="@string/action_suggest"
        android:orderInCategory="8"
        app:showAsAction="ifRoom"/>

    <item android:id="@+id/action_waiterlogin"
        android:title="@string/action_waiterlogin"
        android:orderInCategory="9"
        app:showAsAction="ifRoom"/>
</menu>

The problem is my search button doesn't show on action bar but the text is showed in overflow. There's plenty of room in my action bar

I'm using "@style/Theme.AppCompat.Light"

Can anyone help me ?

like image 760
aacanakin Avatar asked May 29 '14 16:05

aacanakin


People also ask

How to add Button in Action bar android?

Add Action Buttons To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is Action bar in android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is ifRoom in Android?

Description. ifRoom. Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom" , the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu. withText.


3 Answers

Please Try to use android:showAsAction="ifRoom|withText" instead of app:showAsAction="ifRoom|withText"

like image 183
EminenT Avatar answered Oct 04 '22 05:10

EminenT


In my case I had to add a few lines to onCreateOptionsMenu.

Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.

app:showAsAction="ifRoom" wasn't working and I removed it without problems.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater  inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        return super.onCreateOptionsMenu(menu);
    }
like image 32
Mario Eraso Avatar answered Oct 04 '22 04:10

Mario Eraso


In case someone else gets into the same problem - I could not get the button to show up until I overrode the onCreateOptionsMenu function and Inflated the menu (I am using com.android.support:appcompat-v7:24.2.1 version)

Here is the code snippet:

 @Override
    public boolean onCreateOptionsMenu (Menu menu){

        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
like image 34
Naunidh Avatar answered Oct 04 '22 03:10

Naunidh