In my android application, I have an item in the menu, and I want the item display all the time with the icon and the text, this is the setting:
<item
android:id="#+id/menu_submit_back_tomap"
android:title="#string/menu_back_tomap"
android:icon="#drawable/ic_action_back"
android:showAsAction="always|withText"/>
However I can only see the icon, the text is not displayed, what's the problem?
BTW, this is the only item in the menu.
It depend on available space if there will be space to display text it will be displayed if not then only icon will be displayed.
" The "withText" value is a hint to the action bar that the text title should appear. The action bar will show the title when possible, but might not if an icon is available and the action bar is constrained for space." - android developer
http://developer.android.com/guide/topics/ui/actionbar.html
it have two reason to not working always option:
there is no more space in actionbar because of too much icons.
if you are using custom theme then you have to use
yourapp:showAsAction="always"
rather than
android:showAsAction="always|withText"
Are you using the support library? If so, you are supposed to do it differently:
Android 4.3 menu item showAsAction="always" ignored
Related
I have added 5 items on toolbar and when I reviewed in medium and small phones then application name is not visible.
How can I set application name to show always and reduce toolbar's items size according to available devices?
If by toolbar you are referring to the app's ActionBar, then what you are looking for is an overflow menu (that three vertical dots option you see on the top right corner of apps). Reading Android's docs on menus might help.
Basically you want to make sure some, or all of your items have the android:showAsAction property set to never or ifRoom as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action1"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/action1_string"/>
<item
android:id="#+id/action2"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/action2_string"/>
</menu>
The key here is the android:showAsAction property, which allows you to specify when should an item be added to the bar and when it should be added to the overflow menu. Possible values are:
ifRoom: Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu.
withText: Also include the title text (defined by android:title) with the action item. You can include this value along with one of the others as a flag set, by separating them with a pipe |. E.g. : android:showAsAction="ifRoom|withText"
always: Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the app bar.
never: Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
collapseActionView: The action view associated with this action item is collapsible. API 14 onwards.
Source.
I am trying to set image in overflow menu items like as in flipkart.But I am unable to set image.Can anybody provide me code snippet for doing this
If you are trying on Android 3.0+, it won't show icons in the menu items. Google has removed them in order to make the menu look similar on different apps. See icon in menu not showing in android and on Android Developers blog.
It is not possible to add icons in overflow menu item as per googles new change, If u still want, try this.
for device with overflow hardware button:
override hardware button click and show a transparent fragment with list view as like over flow menu in samsung devices(S3, s4) with icon.
for devices without hardware button:
use sub menu to look as menu. google still supports icon in sub menu.
That's how it's supposed to work.
If the icon shows in the action bar, it will only show the icon (with an optional title by using android:showAsAction="always|withText" or android:showAsAction="ifRoom|withText").
If the icon does not show in the action bar, it will only show in the overflow menu as text. Note that there is no "withIcon" enum value for the android:showAsAction attribute.
I am reading about action bars in BusyCoder's Android guide. There is an example:
<item
android:id="#+id/add"
android:actionLayout="#layout/add"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="#string/add"/>
What is the point to have both android:actionLayout and android:icon defined? What are the conditions when the icon would show up?
Thanks for helping out Android noob
What are the conditions when the icon would show up?
I can't rule out the possibility that the action bar will decide that there is not enough room for the actionLayout, but there is enough room for an action item, which would therefore show the icon. Also, with ActionBarSherlock, the icon is shown even in the overflow on Android 2.x devices, and so again if the actionLayout would be too large, the icon might get used. Also, if I used ifRoom|collapseActionView instead of just ifRoom, the icon would be used.
That being said:
My sample does not actually watch for this theoretical action item, should it appear, to do anything if the user clicks on it
I am guessing at possible action bar behavior, as I have not tried any experiments to force the action bar to decide whether there is enough room for all actionLayout-configured items
here android:icon is defined to supply an icon for your menu item. This is always optional but it is recommended to have an icon for the menu item.
android:actionLayout is defined to supply a custom view for the menu item again this is optional but if u need some Image Views, Text Views, Edit Text Views etc. in your menu item, this is the way to go.
If you need more help see these guides. These I think will be more helpful to you.
Action Bar Android Developer Guide
Menu Resource Android Developer Guide
Also take a look at this example. This may give you a much better idea about android Action bars.
vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example
Thank You!
Instead of using an ActionLayout to replace the ActionBar when pressed a MenuItem. I want that ActionLayout to overlap on the ActionBar.
Since most of the applications use this behavior I thought its the standard SearchView widget.
And since my application is for API 10 and more, i couldn't use SearchView, hence used latest ActionBarSherlock library(4.2.0), where Jake added SearchView.
The problem is, even here its like replacing the initial menu items with the ActionViewClass.
The reason i want the ActionLayout to overlap but not replace the ActionBar is, I need to have 3 MenuItems in the ActionBar and if i keep all of them as
android:showAsAction="always|collapseActionView"
they are even visible when ActionLayout is shown, which gives little width for the ActionLayout
if i am using
android:showAsAction="ifRoom"
the last icon is missing. Only 2 icons are visible.
Thank You
If you are having physical menu button (e.g. Nexus S) in your device then the dot line will not show. When you press the Menu button it’ll show up.
You may use something as this Use android:showAsAction="never" to force an action item into the overflow.
Does anyone know how to add "menu" text under the dots menu icons in action bar?
Also, how can I make my option menu items to display the icon in Android 4.0?
You can't add text under the default menu icon in the 4.0 Action bar, you do however have two alternatives
1) You can create your own icon that includes the text 'Menu' (Unfavourable)
2) Create a custom menu item with the dots image and the properties so that the text appears next to it.
android:showAsAction="always|withText"
Take a look at The Android SDK Documentation on Action Bar to see how to create custom items and make them perform tasks.