In an Android app, we have an option menu item called "Contact", which we'd prefer to be shown as an action in the action bar (instead of hidden under the three-dot menu). So we use
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
However when we do that, the "CONTACT" action takes away room from the activity's title. So the title gets cut off with ellipses:
Instead, we'd like the title string to take priority over "IF_ROOM" actions for space in the action bar. In other words, if there's not room to show both the title string and the "CONTACT" action, hide the "CONTACT" action under the three-dot option menu.
Is there a way to do this?
P.S. I realize there are other ways to save space in this action bar image, e.g. removing the "up arrow," shrinking some margin, etc. That may be helpful, but this question is not about other ways to save space.
in your menu add android:showAsAction="never"
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- menu -->
<item android:id="#+id/contact_id"
android:icon="#drawable/ic_action_contact"
android:title="#string/action_contact_id"
android:showAsAction="never" />
<!-- menu -->
</menu>
Or in code
MenuItem.SHOW_AS_ACTION_NEVER
Related
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 am expecting this kind of output
I can add menu items on a toolbar but no idea how to show a list by clicking one of those items.
Specifically, I mean if I click one of those menu items,
it should pop up a dialog/list just shown in above image.
A little guide might help me pass through this confusion.
PopupMenu is what you're looking for: http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
If you want action overflow icon with hidden menu items set in your inflated menu.xml file showAsAction to never:
<item
android:id="#+id/rate_me"
android:title="#string/rate_me"
android:showAsAction="never"
/>
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
I have this screen structure in my app:
Which is tremendous waste of area. Mainly in the system bar and the action top bar.
I would like to:
Move the app logo and title to the system area
Narrow the height of the tabs
Move the menu to the bottom action bar
To get something like this:
You can do one thing remove the title and appicon from this TabLayout.
Use for your activity in androidmanifest.xml
android:theme="#android:style/Theme.NoTitleBar"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Using this parameters your layout will be looking like this see it
I managed to hide the main action bar. As it is described in Android Developer's site:
If you'd like to hide the main action bar at the top, because you're
using the built-in navigation tabs along with the split action bar,
call setDisplayShowHomeEnabled(false) to disable the application icon
in the action bar. In this case, there's now nothing left in the main
action bar, so it disappears and all that’s left are the navigation
tabs at the top and the action items at the bottom, as shown by the
second device in figure 3.
That's necessary, but not enough, because the main action bar still shows the title and the menu icon on the right. Two more steps are required:
Hide the icon and Title
setDisplayShowTitleEnabled(false);
Do not create a menu in onCreateOptionsMenu()
Now the main action bar is empty and it indeed dissapears. The tabs still show. The menu is lost, but you can create a button somewhere in your screen to act as menu, even you can use the same icon.
Tab Height
And I found out that the tab height can be controlled through XML style. Adding this to the styles.xml inside the app style tag
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarSize">35dp</item>
</style>
System Bar
It seems to be that the system bar is, as its name suggest, for system only use, not user. I did not find any clue to whether the elements in there can be modified (exception being notification icons from services)