Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat_v7 Toolbar as actionbar not showing 'always' actions from menu, but API Toolbar does

After 2 days of struggling with the new API 21 Toolbar and appCompat_v7, I think I found a bug on it. If you have 2 actions on your menu like this:

<item
    android:id="@+id/action_test"
    android:showAsAction="always"
    android:icon="@drawable/ic_launcher"
    android:title="@string/action_settings"/>

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

and a appCompat toolbar defined like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.toolbar.MainActivity" >

    <android.support.v7.widget.Toolbar 
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:id="@+id/toolbar">
    </android.support.v7.widget.Toolbar>

</RelativeLayout>

after inflating (or setting the setSupportActionBar method)

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("esurance");
setSupportActionBar(toolbar);

you will get the toolbar menu without your action icon, it will display it on the overflow menu.

But, if you use the Toolbar class from API 21, it will show your actions as defined on your menu layout...

<Toolbar 
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:id="@+id/toolbar">
</Toolbar>

Maybe I'm missing something here, but so far, I've been unable to display actions outside the overflow menu using appCompat. Any help on this will be much appreciated.

enter image description here

like image 620
Roisgoen Avatar asked Oct 29 '14 18:10

Roisgoen


2 Answers

Per the Action Bar training, you have to use the app:showAsAction attributes rather than the android:showAsAction attribute:

Notice that the showAsAction attribute above uses a custom namespace defined in the <menu> tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

So your menu file should look like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_test"
        app:showAsAction="always"
        android:icon="@drawable/ic_launcher"
        android:title="@string/action_settings"/>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"
        android:title="@string/action_settings"/>
</menu>
like image 156
ianhanniballake Avatar answered Oct 17 '22 18:10

ianhanniballake


put

xmlns:app="http://schemas.android.com/apk/res-auto" int your menu tag

like this

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

then use app:showAsAction = "always"

Remember that use app:showAsAction in all menu item not android:showAsAction

`

like image 3
Singhak Avatar answered Oct 17 '22 16:10

Singhak