Search code examples
androidappcompat-v7-toolbar

Appcompat toolbar showAsAction doesn't work properly


My device allows to show up to 3 icons in the taskbar, if I exceed this number all the icons are hidden even if I have showAsAction="always" or showAsAction="ifRoom".

This is my code:

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
    <group android:id="@+id/groupActions">
        <item
            android:id="@+id/action_admin"
            android:orderInCategory="0"
            android:title="@string/buttonAdmin"
            app:showAsAction="always" />

        <item
            android:id="@+id/action_readMode"
            android:orderInCategory="1"
            android:title="@string/buttonReadModeOn"
            android:icon="@drawable/ic_read_mode_24dp"
            app:showAsAction="always" />
    </group>

    <group android:id="@+id/groupActions">
        <item
            android:id="@+id/action_schedule"
            android:orderInCategory="2"
            android:title="@string/buttonSchedule"
            android:icon="@drawable/ic_today_24dp"
            app:showAsAction="ifRoom" />

        <item
            android:id="@+id/action_report"
            android:orderInCategory="3"
            android:title="@string/buttonReport"
            android:icon="@drawable/ic_list_check_24dp"
            app:showAsAction="ifRoom" />

        <item
            android:id="@+id/action_logout"
            android:orderInCategory="4"
            android:title="@string/buttonLogout"
            app:showAsAction="never" />
    </group>

    <group android:id="@+id/groupInfo">
        <item
            android:id="@+id/action_help"
            android:orderInCategory="5"
            android:title="@string/buttonHelp"
            android:visible="false"
            app:showAsAction="never" />

        <item
            android:id="@+id/action_contact"
            android:orderInCategory="6"
            android:title="@string/buttonContact"
            app:showAsAction="never" />

        <item
            android:id="@+id/action_about"
            android:orderInCategory="7"
            android:title="@string/buttonAbout"
            app:showAsAction="never" />
    </group>
</menu>

Here the code on MyActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuCompat.setGroupDividerEnabled(menu, true);
    this.menu = menu;
    MenuItem item = menu.findItem(R.id.action_readMode);
    if (item != null) {
        tintReaderModeIcon(item);
    }
    return super.onCreateOptionsMenu(menu);
}

My idea is to show the two icons with showAsAction="always" and the icon that has showAsAction="ifRoom" with the highest priority.

This is the desired result: enter image description here

And this is what I get: enter image description here

Please help to clear this out.

EDIT: Thanks to Denis95's response I have managed to solve half of the problem. Now the icons with showAsAction="true" are displayed correctly.


Solution

  • As i can see from the resource file, you're grouping 5 items together. Try to put the items you want to always show on the AppBar outside that group. It should do the trick.