ShowAsAction = "always" is ignored in the Toolbar

After switching to the toolbar, there is a problem with the menu icons. Although I set android: showAsAction = "always" for the menu item, it does not show the icon, I can only find it by clicking on the popup window icon.

This is myActivity

public class myActivity extends AppCompatActivity{ ......... public void onCreate(....){ ............. Toolbar toolbar = (Toolbar) findViewById(....); setSupportActionBar(toolbar); } ............ public boolean onCreateOptionsMenu(Menu menu{ getMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } ............. } 

menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/settings" android:icon="@drawable/settings" android:title="settings" android:showAsAction="always" /> <item android:id="@+id/help" android:icon="@drawable/help" android:title="help" android:showAsAction="never" /> </menu> 

Both settings and help icons are only available in the pop-up menu. So, how to show the settings icon in the toolbar?

+5
source share
2 answers

Replace android:showAsAction with app:showAsAction . You also need to add xmlns:app="http://schemas.android.com/apk/res-auto" next to the existing xmlns element in the root element.

+15
source

There is a slight change with AppCompat. If you use a lint, he will complain about it. Enter this:

 <?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"> <item android:id="@+id/someId" android:title="@string/someText" app:showAsAction="always"/> </menu> 

You need to declare the namespace "application" and reference it.

+1
source

All Articles