Probably you are missing required namespace:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_add_size"
android:title="@string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="@android:drawable/ic_menu_add" />
</menu>
Replace [yourapp]
with your app name or any namespace your heart desires everywhere.
Other things worth checking:
ActionBarActivity
Check if the issue persists.
Android reference documentation: Adding Action Buttons. Here is the relevant text:
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)
Figured out myself. With the support library v7 the showAsAction should go under a custom namespace like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_add_size"
android:title="@string/menu_add_item"
android:orderInCategory="10"
balloonberry:showAsAction="always"
android:icon="@android:drawable/ic_menu_add" />
</menu>
Also make sure that you use correct inflater in ActionBarActivity.onCreateOptionsMenu() method.
Correct solution:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_example, menu);
Incorrect solution:
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu_example, menu);
For Fragments
Menus with custom namespace will prevent showAsAction from showing.
Using "android:" prefix for showAsAction will work, even though Android Studio will remark you should use a custom name space.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_add_checkin"
android:title="Add Checkin"
android:orderInCategory="10"
android:showAsAction="always"
android:icon="@android:drawable/ic_menu_add"/>
</menu>
This is using Android SDK 22 and Support v4 fragments, in case that makes any difference.
Got the same problem, but on Android 5. I have 3 items but OS ignored my attribute "always" and showed only 2 items. Here my solution:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu()");
inflater.inflate(R.menu.your_menu, menu);
for (int j = 0; j < menu.size(); j++) {
MenuItem item = menu.getItem(j);
Log.d(TAG, "set flag for " + item.getTitle());
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
Also make sure that you have the correct path for the namespace. It will not give you an error message if it's wrong.
I had
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/res-auto">
instead of
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
All I knew was that it wasn't working. Not sure how I managed to forget the /apk part of the path, but it happened. No error message, just an elusive bug to track down.
In my case, I had to remove from my app's build.gradle compile 'com.android.support:appcompat-v7:21.0.3'.
Notice: My min sdk = 14, and created project by android studio inserted my unnesessary dependancy.
After this replace you can write android:showAsAction="always"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With