I'm writing an android application (sdk 8 to 15) and I'm dealing with the menu. I read a lot of things on it to understand, but I 'm stuck on one point :
Apparently since sdk 10, hardware menu button isn't supported, now we have to use the action bar. Ok. But in my app I want to have this kind of menu :
Or this :
... which are not hardware menu button but seems to be 'native action bar'. All I can find on the web is an action bar on the top, with the title of the app and a logo on the left.. which take a lot of place.
I'm so lost with all those sdk versions and I just want to integrate a simple menu like above, how should I proceed ?
I have this in my code, but :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragments_slider, menu);
return true;
}
... but no menu appears with android 4 as it should be in the first or second image.
Check out the Menu Resource in the Android API Documentation and especially:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" |
"collapseActionView"]
The example XML from the documentation is a good resource also for exactally how to lay it out:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1"
android:title="#string/item1"
android:icon="#drawable/group_item1_icon"
android:showAsAction="ifRoom|withText"/>
<group android:id="#+id/group">
<item android:id="#+id/group_item1"
android:onClick="onGroupItemClick"
android:title="#string/group_item1"
android:icon="#drawable/group_item1_icon" />
<item android:id="#+id/group_item2"
android:onClick="onGroupItemClick"
android:title="#string/group_item2"
android:icon="#drawable/group_item2_icon" />
</group>
<item android:id="#+id/submenu"
android:title="#string/submenu_title"
android:showAsAction="ifRoom|withText" >
<menu>
<item android:id="#+id/submenu_item1"
android:title="#string/submenu_item1" />
</menu>
</item> </menu>
You then need to add to your Manifest:
android:uiOptions="splitActionBarWhenNarrow"
That will move your buttons down to the bottom, leave the title in the actionbar and if you have more than what can fit in the screen you get the little three dot | on the right of the lower actionbar. This only really works in Portrait mode though, unless you have quite a few menu items... or longer text strings (I think...)
ActionBarSherlock puts your menu stuff in the ActionBar across all supported platform versions.
Add it as library project and make sure that you import the Sherlock Menu and extend SherlockActivity.
Related
I have tried setting the:
android:showAsAction=".."
to every one of these:
ifRoom, never, withText, always, collapseActionView
but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button.
Here is a picture of the menu now :
<item android:id="#+id/smth1"
android:title="#string/smth1"
android:showAsAction="always"
android:orderInCategory="1" />
I have even tried adding this:
android:uiOptions="splitActionBarWhenNarrow"
into application manifest file, but with no positive result (nothing changed).
I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result.
If my question seems to be unclear, here is a picture of a menu, which I would like to have:
Thanks for any help.
You need to use the compatibility namespace (see here)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always"/>
</menu>
Once you're using that you can use as many menu buttons as will fit. You're no longer limited to just two buttons + overflow showing.
You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar.
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!
How do you manage Icons with ABS and Gingerbread? As you can see from the Screenshots, I have a menu item "Help", that sits in the action bar, when there's room for it. (when nothing is selected). The icon looks good so far. But when it moves into the options menu, I probably should use another icon for better visibility :)
How do you normally do that? Any ideas? This is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/new_button"
android:showAsAction="always"
android:title="new"
android:icon="#drawable/content_new_calendar"/>
<item
android:id="#+id/share_button"
android:visible="false"
android:showAsAction="ifRoom"
android:title="share"
android:icon="#drawable/social_share"/>
<item
android:id="#+id/help_button"
android:showAsAction="ifRoom"
android:title="help"
android:icon="#drawable/action_help"/>
</menu>
I know how to create different menus for various api levels, etc. But here, the same phone is showing the icon in the action bar and in the options menu. The simplest solution is to set android:showAsAction="never", but from user reviews I learned, that the App is difficult to understand (especially the Widget-Part), so I would love to have the help menu visible. Any ideas?
P.S. I know that the App is ugly and unfinished. It's work in progress and will be polished :)
Try using the force overflow option like so:
<item name="absForceOverflow">true</item>
I have 3 menu icons on the menu bar, but everytime it only shows 2 icons, the last one is in nowhere.
my questions are:
1. there is enough space for 3 icons, why only 2 are shown?
2. if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
Below is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom" />
<item android:id="#+id/add_homework"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom" />
<item android:id="#+id/set_groupid"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="ifRoom" />
</menu>
and this snippet is in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I have 3 menu icons on the menu bar
For the purposes of this answer, I am assuming that by "menu bar" you are referring to the action bar.
but everytime it only shows 2 icons, the last one is in nowhere
The last one is available by pressing the MENU button, for devices (or emulators) that have an off-screen MENU button.
there is enough space for 3 icons, why only 2 are shown?
Presumably because Android disagrees with your assessment of whether or not there is enough space for 3 icons.
if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
I have no idea why you think forcing the 2nd icon -- which, by your admission, fits -- into the overflow menu would be a good idea. The 3rd menu item is in the overflow menu, which is accessed via the MENU button on devices that have one or a three-vertical-dots button on the action bar for devices that lack a MENU button.
I encountered this same issue and resolved it by doing two things, although I think the main reason is the second item. I was using the Android Asset Studio to create icons for my Actionbar menu.
In Android Asset Studio, I set the icon to "trim".
In my menu/activity_main.xml definition file (or whatever you call your menu definition file) I defined my menu icons with android:showAsAction="always"
It looked something like this:
<item android:id="#+id/menu_test"
android:icon="#drawable/ic_menu_test"
android:title="#string/menu_test"
android:showAsAction="always" />
Android action bar compat
Is it possible? On older devices (pre 3.0) the items that don't fit the action bar are only shown when the menu key is pressed, I want these items to be grouped in the actionbar's overflow menu.
The action overflow menu is only available when there is no hard menu button available on the device. I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here.
There is an action bar library written by Jake Wharton called ActionBarSherlock. Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this.
Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. I would suggest just looking into using his library all together, as it is very well done.
If you choose to use Jake's library, look into setting up the Activity theme as #style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices.
Edit2: Using ForceOverflow theme causes issues (example #1) on devices with hardware menu button. Thus, Jake Wharton is going to remove ForceOverflow in the future versions.
Okay, this is simple but hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
Honestly it shouldn't matter if you have the actionbar split or not but I prefer it.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
Following LeviRockerSk8er's suggestion I've forced to have a overflow menu in the action bar like this:
This is the code for "menu.xml":
<item
android:id="#+id/web_clasica"
android:icon="#drawable/ic_action_web_site"
android:showAsAction="ifRoom"
android:title="#string/menu_web"
/>
<item
android:id="#+id/overflow_fijo"
android:icon="#drawable/ic_action_core_overflow"
android:showAsAction="always"
android:title="#string/menu_email"
>
<menu>
<item
android:id="#+id/email"
android:icon="#drawable/ic_action_new_email"
android:showAsAction="ifRoom"
android:title="#string/menu_email"
/>
<item
android:id="#+id/share"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="#string/menu_share"
/>
<item
android:id="#+id/about"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_action_about"
android:title="#string/menu_about"/>
</menu>