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.
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"
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.
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.
When you put a lot of action items on the action bar, some of them get to be inserted into the overflow menu, or become available by clicking on the menu button (if present).
Is there a way to be notified when this occurs? Maybe even know which items got to be into the overflow menu (or available via the menu button) ?
Again, I don't ask how to put items there and I don't need to get explanation about how to define action items. I ask how to know when and which items get to be there.
You cannot decide which item to be into Action overflow menu but you can decide which to be shown always on Action bar.
You can also use "always" to declare that an item always appear as
an action button. However, you should not force an item to appear in
the action bar this way. Doing so can create layout problems on
devices with a narrow screen. It's best to instead use "ifRoom" to
request that an item appear in the action bar, but allow the system to
move it into the overflow when there's not enough room. However, it
might be necessary to use this value if the item includes an action
view that cannot be collapsed and must always be visible to provide
access to a critical feature.
http://developer.android.com/guide/topics/ui/actionbar.html
I have 8 menu items. I am using menu.add() method to create all 8 menu items. Now when i run the app. The menu is shown in two rows with last item of second row being more and then clicking more a single list of the remaining 3 items is shown. I want all the 8 menu items to be shown in the single list without more button and rows. How can i achieve this functionality. Any help is appreciated in advance.
Thanks
According to android docs
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
I suggest to use Action bar or Implement Sherlock library to have a vertical list of menu in your project.