android.support.v7 with `ActionBarActivity` no menu shows
Try pressing the MENU button on your device or emulator, and see if they appear in the overflow.
If they do, then the problem is that your <menu>
XML needs to change. Menu XML that works with ActionBarSherlock and the native API Level 11+ action bar will not work with the AppCompat action bar backport.
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="@+id/item_menu_ok" android:icon="@drawable/ic_action_ok"
android:title="@string/ok" yourapp:showAsAction="always"></item>
<item android:id="@+id/item_menu_cancel" android:icon="@drawable/ic_action_cancel"
android:title="@string/cancel" yourapp:showAsAction="always"></item>
</menu>
And you would need to use the same yourapp
prefix for anything else related to the action bar (e.g., yourapp:actionLayout
).
You can see this covered in the action bar documentation.
nrofis
Updated on July 14, 2022Comments
-
nrofis almost 2 years
In the new update Google has released a new API support library, that supports the ActionBar in API level 7+.
I used ActionBarSherlock until this update and I wrote the code to load the menu:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main, menu); return true; }
and the menu file:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item_menu_ok" android:icon="@drawable/ic_action_ok" android:title="@string/ok" android:showAsAction="always"></item> <item android:id="@+id/item_menu_cancel" android:icon="@drawable/ic_action_cancel" android:title="@string/cancel" android:showAsAction="always"></item> </menu>
To set up the menu buttons on the action bar. This code worked perfectly with ActionBarSherlock. But when I changed the action bar to the new support library, the buttons are not shown in the action bar. Even if they are set as
android:showAsAction="always"
. And when I debug the code, the functionmenu.getSize()
return 2, and that is correct, but no buttons are shown..Why are the buttons not shown in the new support library?
-
nrofis almost 11 yearsYea, the problem was the
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
. This is change for the Support v7? Cause inActionBarSherlock
my old code worked perfectly -
CommonsWare almost 11 years@nrofis: I have no idea why AppCompat cannot use
android:showAsAction
the way ActionBarSherlock can. -
Aiden Fry almost 11 yearsThanks this is a tiny and weird change that i managed to overlook!
-
Wenger almost 11 yearsThanks Mark, I was looking for this as well. To clarify, does "yourapp" include the "com.xxx.package" or just "package"?
-
Aiden Fry almost 11 yearsNo replacement its literally "yourapp"
-
CommonsWare almost 11 years@Wenger: The
yourapp
on the attributes needs to match theyourapp
namespace declaration in the root element. AFAIK, so long as those names are the same (and are valid for XML), they can be whatever string you want. -
Adam Johns over 10 yearsNote if you are using Android Studio: Make sure to
Build > Rebuild Project
. I couldn't figure out why mine wouldn't work, but it was simply because it needed a rebuild. -
Vivek Todi over 10 yearsFor some reason, it still doesn't appear on the top right corner of my app but it shows at the bottom middle of the string. @CommonsWare Any Idea why? I have done everything you have mentioned here.