I am having 5 items in the menu, and I only want to show 2 at the time.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_start"
android:icon="#drawable/ic_action_start"
android:orderInCategory="93"
android:showAsAction="ifRoom|withText"
android:title="#string/menu_start"/>
<item
android:id="#+id/menu_filter"
android:icon="#drawable/filter_icon"
android:orderInCategory="94"
android:showAsAction="ifRoom|withText"
android:title="#string/menu_filters"/>
<item
android:id="#+id/menu_refresh"
android:icon="#drawable/ic_menu_refresh"
android:orderInCategory="95"
android:showAsAction="never"
android:title="#string/menu_refresh"/>
<item
android:id="#+id/menu_delete_all"
android:icon="#drawable/ic_menu_delete"
android:orderInCategory="96"
android:showAsAction="never"
android:title="#string/menu_delete_all"/>
<item
android:id="#+id/menu_show_hidden"
android:orderInCategory="97"
android:showAsAction="never"
android:icon="#drawable/ic_menu_show_hidden"
android:title="#string/menu_show_hidden"/>
</menu>
When I test this on Galaxy Nexus, or any device that doesnt have menu button, it creates an overflow menu, however it does not add to be the third one as I would liked, but it removes my second icon and replaces it.
How do I have to setup the actionbar, to have 2 icons on devices without menu button, and 2 + (overflow) on devices that have?
Thanks !
Related
I have a class that extends FragmentActivity. No I want to show my menu items at the top of the screen like in actionbar. How can I do this ?
I have created the menu items and they are visible hen the hardware button for menu is pressesd but I want to sho them at the top of the screen.
Basically, this happens in devices with physical menu button. In devices without that button, all options are displayed in the action bar.
You can force a menu item to be displayed in the action bar by addind "showAlways" to the item that you want.
showAsAction=“always”
An small example
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" />
</menu>
You must need to ensure that you have enough space for the menu items.
To ensure that I'll have enough space, I use to create a menu with only one item. Then, inside this item, I create another menu. This way, only one button is displayed and all items appear in a floating pop up menu. The main idea is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/overflow_item"
android:icon="#drawable/ic_overflow_icon"
android:title="#string/options"
android:showAsAction="always" />
<menu>
<item android:id="#+id/action_clear"
android:title="#string/clear"
android:showAsAction="always" />
</menu>
</menu>
I have an app with a NoActionBar theme. In my main activity I have an options menu that I created manually on the top of the screen (or by utilizing the built in device's options button).
In this main activity, I have a fragment with a listView where I apply the action mode long click functionality, to show the contextual action bar (CAB) for further user options.
Now, I try adding an options item to my CAB so it will contain some options like selecting all items in the listView, but since it's an item of the CAB, I can't really show the popup menu like in a regular activity. Further more, I want all the options menu callbacks (such as onOptionsItemSelected) to stay in the context of the CAB, in order to be able to continue to perform actions on the CAB.
Here's the code of my CAB:
<?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_delete"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_overflow"
app:showAsAction="always"
android:orderInCategory="200"
android:icon="#drawable/ic_action_overflow"
android:title="Options"
android:visible="false"/>
</menu>
Apparently I missed the built in feature of the CAB - a built in overflow menu that collapses some of the actions' items once the screen is too small to show them all.
Another manipulation that needs to be done in order to always collapse certain actions under that overflow menu is to set for each one of them:
android:showAsAction="never"
app:showAsAction="never"
So, say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be collapsed always under the built in overflow menu, we'll set this in the CAB's xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
mm:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_select"
android:orderInCategory="200"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/select_all"/>
<item
android:id="#+id/action_add"
android:orderInCategory="300"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/button_add"/>
Basically I am inflating a menu
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
globalMenu = menu;
getSupportMenuInflater().inflate(R.layout.menu_refresh, menu);
return super.onPrepareOptionsMenu(menu);
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_refresh"
android:showAsAction="never"
android:title="#string/refresh"/>
<item
android:id="#+id/menu_24hours"
android:showAsAction="never"
android:title="#string/twentyfour_hours"/>
<item
android:id="#+id/menu_1week"
android:showAsAction="never"
android:title="#string/one_week"/>
<item
android:id="#+id/menu_1month"
android:showAsAction="never"
android:title="#string/one_month"/>
<item
android:id="#+id/menu_3month"
android:showAsAction="never"
android:title="#string/three_month"/>
<item
android:id="#+id/menu_6month"
android:showAsAction="never"
android:title="#string/six_month"/>
</menu>
whats happening, is that none of them are showing in 3 dots format.In ice cream sandwich you have to click the button menu on hardware, in nexus it shows 3 dots..
i need everywhere to have it as 3 dots, no device uniqueness.
However if i do this in my menu...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh_default" />
</menu>
this shows up in actionbardsherlock on top, on all devices. no menu hardware key.
Try creating dummy actionButton which have 3 dots as the icon. This is what I do if I need 3 dots button to be shown on all devices (Especially Samsung devices which have hardware menu keys).
Note that this is actually a hack
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_dummy_overflow"
android:icon="#drawable/ic_action_ic_overflow"
android:showAsAction="always"
android:title="#string/more">
<menu>
<item
android:id="#+id/menu_open_browser"
android:icon="#drawable/ic_action_ic_browser"
android:showAsAction="never"
android:title="#string/opeinInBrowser">
</item>
</menu>
</item>
</menu>
This is something that's decided by the Android OS build that is running. The bad news is that it means you have little control over it. The good news is that it means the user will see the same behaviour on every app on their phone, so they will be expecting it anyway.
You could mimic the behaviour by manually adding an overflow item to your menu and putting your overflow items into a sub-menu of that item. The downside of this is that the number of items you show is fixed. You won't get the benefit of a tablet in landscape mode showing more items in the ActionBar
Im using the ActionBarActivity on my activity and inflating the following menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="never" />
</menu>
The second menu item drawable that i want to appear as a menu and not in the action bar doesn't not appear in my S4 (running 4.3) and also on lower devices running 2.3.7
Just update your code with following code.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:microecs="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/description_refresh"
android:orderInCategory="1"
microecs:showAsAction="ifRoom" />
<!--TODO: get a good logout icon-->
<item
android:id="#+id/menu_logoff"
android:icon="#drawable/ic_action_logout"
android:title="#string/description_logoff"
android:orderInCategory="2"
microecs:showAsAction="ifRoom" />
</menu>
Whenever you are using never then it dont place that item in the Action Bar.
For about menu refer this link.
This is not possible by design. Overflow menus from the action bar don't show icons - that's just a design decision Google made.
You can check more, e.g. here: Displaying icon for menu items of Action Bar in Honeycomb android 3.0
I'm using ActionBarSherlock to provide a unified UI for my Android 2.x and Android 4.x users. I have a menu with 6 items.
On a 2.x, 480px wide device with an hdpi screen, only 5 of the icons show. The device has a hardware Menu button, but when I tap it, nothing shows up. I expected it to popup and show the Action that wasn't able to fit.
I expect either the three-vertical-dots button to appear in the ActionBar to show a dropdown with the overflowed actions or I expect the physical Menu button to show the old style menu with the overflowed actions.
What am I missing or what am I doing wrong?
Here is my defined menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/add"
android:icon="#drawable/ic_menu_btn_add"
android:showAsAction="always"
android:title="Add"/>
<item
android:id="#+id/calculateNPV"
android:icon="#drawable/menu_icon_npv"
android:showAsAction="always"
android:title="NPV"/>
<item
android:id="#+id/calculateIRR"
android:icon="#drawable/menu_icon_irr"
android:showAsAction="always"
android:title="IRR/YR"/>
<item
android:id="#+id/send"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share_pdf"/>
<item
android:id="#+id/graph"
android:icon="#drawable/ic_menu_gallery"
android:showAsAction="always"
android:title="#string/view_cashflow_diagram"/>
<item
android:id="#+id/deleteReorder"
android:icon="#drawable/ic_menu_clear_playlist"
android:showAsAction="always"
android:title="#string/delete_reorder_cashflows"/>
</menu>