I have the following issue with my app, specifically with the Action Bar:
and the ID (indicated with the red rectangle) but since the size of the Action Bar nor the icons contained in cannot be changed due to esthetic reasons, I would like to display a text with the whole number and ID when the users press in the section I've highlighted.
Currently, I'm setting the text like this(delivery var and idDelivery are Strings):
ActionBar ab = getActionBar();
ab.setTitle(delivery);
ab.setSubtitle("ID:" + idDelivery);
Any ideas?
Change MenuItems showAsAction attribute to ifRoom:
<item app:showAsAction="ifRoom"
.../>
From docs:
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.
Thus, the title of your Toolbar would be given higher priority, and MenuItems would be displayed only when there is enough room for them, otherwise they would be left to be displayed under overflow icon popup menu.
Related
In the Menu layout file, we give showAsAction property with different defined values.
What if there is no place for items and we give showAsAction="always"?
If the item displays in the overflow menu what makes it different from showAsAction="ifRoom"?
If there is no place for items and you give showAsAction="always", ActionBar title starts to shrink, and creates more room for items:
If you use showAsAction="ifRoom" in this case, menu items move to the bottom of more button, because there is no room for those menu items:
If you're giving each menu item to showAsAction="always", so every single menu item will be forced to show up in the action bar. This will cause other items in your action bar like the toolbar title and navigation button to have a smaller width. Because of this bug, Android Studio always suggested using ifRoom properties.
All menu items with showAsAction="always"
The number of option menu items can change on runtime in my app, depending on the actions of the user.
I am using this right now for my menu items :
app:showAsAction="ifRoom"
If there is enough room in some cases there are 4 menu items visible in the actionbar. Is there a way to limit the visible menu items?
In my case, I want maximum 2 menu items visible in the actionbar, the additional entries should be in the "more" section.
You can force maximum two visible MenuItem as action buttons on the actionbar by setting:
app:showAsAction="never"
for all the menu items, except the first two.
As mentioned in the Android docs:
never : Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
Currently you're using ifRoom keyword, which will show the menu item in the action bar if there's enough space available for it. So, you won't be able to force max two items using this. From docs:
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.
P.S. : What you're calling more section is referred as overflow menu in the above text and in Android.
What is the difference between the following in menus in android as I am getting the same output as when using never when using any of these:
1.withText
2.collapsActionView
Can anyone explain?
You have the answer on Google Developer site here.
withText for example, you can use in form android:showAsAction="always|withText" and it will try to show always text along with icon if there is room for it.
collapseActionView for example, you can use in form android:showAsAction="always|collapseActionView" to only show that item when menu is in collapsed view.
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 |.
collapseActionView
The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
Introduced in API Level 14.
There are five values associated with showAsAction. There differences are below.
ifRoom - only place an item in the app bar if there is room. If there are multiple items with "ifRoom" flag a few will be shown as actions in the app bar and the rest will be visible in an overflow menu.
withText - shows text defined in the title for the item without an icon. You can add one more flag using pipe "|". eg- "always|withText".
never- It will always show the item in the overflow menu instead of the app bar.
always- It will always show the item in the app bar. Avoid using this because if there are multiple items with the same flag it will overlap with other UI in the appbar.
collapseActionView- The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
Introduced in API Level 14.
For more info visit menu-resource
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.
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