The action bar shows a drop-down menu when only one item is displayed in the menu.

I use the application compatibility theme in my application, and on the action bar I have a menu with one item that should appear as an icon, but instead I get a menu button like this

enter image description here

Here is my xml menu code:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_play" android:icon="@drawable/play" android:orderInCategory="100" android:showAsAction="always"/> </menu> 

And here is the code on how I initialize the action bar:

 getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setTitle(title); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(false); 

and show the menu:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.names, menu); playStop = menu.findItem(R.id.action_play); return true; } 

So can someone help me display the menu item as it should?

thanks

+5
source share
2 answers

Using the support library, android:showAsAction will not complete its task. Therefore, your menu declaration is incorrect. From the documentation

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" yourapp:showAsAction="ifRoom" /> ... </menu> 

If there is not enough space in the action bar for an item, it will appear in the action overflow.

Using XML Attributes from the Support Library

Note that the showAsAction attribute above uses its own namespace defined in the tag. This is necessary when using any XML-code attributes defined by the support library, since these attributes do not exist in the Android platform on older devices. Therefore, you should use your own namespace as a prefix for all attributes defined by the library support.


So your xml menu should be

 <?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/action_play" android:icon="@drawable/play" android:orderInCategory="100" app:showAsAction="always"/> </menu> 
+8
source

You need to use

 app:showAsAction="always" 

but not

 android:showAsAction="always" 
0
source

All Articles