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 function menu.getSize() return 2, and that is correct, but no buttons are shown..
Why are the buttons not shown in the new support library?
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.
I'd like to add a little to the answer.
If you want to see both text and an icon, please use withText in showAsAction
I've just tested it; when I used always or ifRoom without withText, I only saw an icon.
Related
I want my refresh item to appear on the top bar. I am extending ListActivity and using the theme android:Theme.Holo.Light.DarkActionBar. The menu is created using:
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="eu.pcas_project.client.android.pa.services.ServiceList">
<item
android:id="#+id/refresh_all_services"
android:title="#string/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
app:showAsAction="always" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_service_list, menu);
return super.onCreateOptionsMenu(menu);
}
If I change app:showAsAction="always" on the menu XML to android:showAsAction="always", this works as I want it to—icon on top bar—but then I get an error saying Should use android:showAsAction when not using the appcompat library. Can it be fixed?
Targeting API 19.
It depends, if you hav a physical button for menu it doesn't appear. but you can try this:
<item
android:id="#+id/refresh_all_services"
android:icon="#drawable/ic_menu_refresh"
android:title="#string/refresh_all_services"
app:showAsAction="withText|ifRoom"
app:actionProviderClass="android.support.v7.widget.ActionProvider"
/>
As said in the question, android:showAsAction="always" works but generates an error.
While the project was not using the appcompat library directly, I discovered that a library imported by said project had com.android.support:appcompat-v7:XXX.YYY.ZZZ as a dependency even though it was not needed. This was added by Android Studio. Once the line was removed and the project rebuilt the error disappeared and the refresh icon appeared on the top right corner instead of on the menu.
My original application was written for Android 2.1. Afterwards, I've added compatibilty library and ActionBar Sherlock.
Now, I would like to present options menu as overflow in the action bar and it works as expected. However, on devices without menu button, I still get default menu bar at the bottom of the screen. Clicking on it opens the options menu from the action bar. See image below:
What is worse, this bar shows even on activities that have no options menu defined.
Regarding my relevant code, there is nothing special about it.
Inflating options menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:icon="#drawable/ic_menu_flag" android:title="#string/I_STR_LANGUAGE" android:id="#+id/menu_lang" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_pin_change" android:title="#string/change_pin" android:id="#+id/menu_pin_change" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_about" android:id="#+id/menu_about" android:title="#string/about_application" android:showAsAction="never"></item>
<item android:icon="#drawable/ic_menu_exit" android:id="#+id/menu_logout" android:title="#string/I_CLOSE" android:showAsAction="never"></item>
</menu>
Application theme inherits from DarkActionBar Sherlock Theme
<style name="AppTheme" parent="Theme.Sherlock.Light.DarkActionBar">
Is there a way to hide system menu bar? Can options menu be presented only from action bar? Can it at least be hidden for activites without options menu?
[UPDATE:] <uses-sdk android:minSdkVersion="7" />
You need to modify your uses-sdk node to target the latest API, like so :
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18"/>
I'm having a problem when I try to set one item in my actionbar as always visible and 4 more icons as dropdown items with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_movies"
android:icon="#drawable/action_search"
android:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/movies"
android:icon="#drawable/action_video"
android:showAsAction="collapseActionView"
android:title="Movies"/>
<item
android:id="#+id/theaters"
android:icon="#drawable/action_location_map"
android:showAsAction="collapseActionView"
android:title="Theaters"/>
<item
android:id="#+id/preferences"
android:icon="#drawable/action_settings"
android:showAsAction="collapseActionView"
android:title="Preferences"/>
<item
android:id="#+id/contact"
android:icon="#drawable/action_about"
android:showAsAction="collapseActionView"
android:title="Contact"/>
</menu>
The result is just the first item showing and the rest are not visible, not even as a dropdown. This is using ActionBarSherlock and a 2.3 Android device.
The question is, how can I get the icons to follow this layout:
EDIT:
The problem I had was because when you are using the actionbar with a device that has a "menu" hardware button the 3-dot dropdown does not shows off, the 4 other items are only displayed if you press the menu hardware button. Does anyone knows if this behaviour can be modified?
Hmmm, maybe I misunderstood, but if you wish to places those remaining four items into the overflow action menu (the 3-dot icon) then using android:showAsAction="never" instead of "collapseActionView" should do it.
...Tried a couple ways, but this did the trick:
Force overflow menu in ABS
I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)
The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("More");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sub.getItem().setIcon(R.drawable.ic_menu);
getSupportMenuInflater().inflate(R.menu.activity_main, sub);
return true;
}
Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.
It works!
I am developing Android 2.1 API 7 app. To implement action bar, I am using ActionbarSherlock library.
Everything goes fine with the sherlock library, I can implement action bar with it in my project with the following code.
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_payment_1"
android:title="#string/new_payment"
/>
<item
android:id="#+id/label_1"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"/>
<item
android:id="#+id/label_2"
android:title="text2"
android:showAsAction="always"/>
<!-- overflow section of action bar -->
<item android:title="title2"/>
<item android:title="title3"/>
<item android:title="title4"/>
</menu>
In my Activity class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
I got action bar successfully with above code. No problem at all on Android 3.2 platform.
BUT the problem is if I run my app on Android 2.1 platform, the action bar has no overflow section on the Action Bar. Why??? Anyone has experienced the same problem when using Sherlock library on old Android platform??
(P.S. "overflow section" of action bar is the right-most part of action bar which hides some items like a popup menu. More info here )
It uses the native options menu as overflow, just as an Ice Cream Sandwich phone would should one be made with a hardware menu key.
Forcing an overflow action item to be on the action bar on pre-4.0 devices will be a feature of version 4 of ActionBarSherlock.
Support for overflow menu for pre-Ice Cream Sandwich devices has been removed from ActionBarSherlock. A good discussion of this can be found here: Force overflow menu in ActionBarSherlock
If I define the following items for my action bar:
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="label"/>
<item android:title="label1"/>
<item android:title="label2"/>
<item android:title="label3"/>
<item android:title="label4"/>
</menu>
In my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
Is there anyway to allow me define certain items move to action overflow part ? and how to do it?
P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.
It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags
E.g.
<item android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/reload_button"
android:showAsAction="always"/>
Here android:showAsAction tells how to handle it. Options are
always
ifRoom
never
withText
You can or options together with the pipe symbol as "always|withText"
See the android docs for action bar for more documentation.
To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.
On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".