Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showAsAction = "always" is ignored in Toolbar

After switching to toolbar there is a problem with menu icons. Although I set for a menu item android:showAsAction="always" it does not show the icon, I can only find it clicking on the popup 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 in popup menu. So how to show settings icon on toolbar?

like image 486
Veka Avatar asked Aug 14 '15 12:08

Veka


2 Answers

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

like image 135
CommonsWare Avatar answered Sep 28 '22 09:09

CommonsWare


With AppCompat there is a little change. If you are running lint it will complain about it. Type 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 "app" namespace and reference it.

like image 22
Thomas R. Avatar answered Sep 28 '22 10:09

Thomas R.