define Action bar overflow items - android

If I define the following items for my action bar:
res/menu/action_menu.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="label"/>
<item android:title="label1"/>
<item android:title="label2"/>
<item android:title="label3"/>
<item android:title="label4"/>
</menu>
In my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
Is there anyway to allow me define certain items move to action overflow part ? and how to do it?
P.S. Action overflow part is the right-most part of action bar which hide certain items like a popup menu.

It's the other way round. You need to explicitly tell the menu which ones you want in the ActionBar and which not by setting the appropriate flags
E.g.
<item android:id="#+id/refresh"
android:title="#string/refresh"
android:icon="#drawable/reload_button"
android:showAsAction="always"/>
Here android:showAsAction tells how to handle it. Options are
always
ifRoom
never
withText
You can or options together with the pipe symbol as "always|withText"
See the android docs for action bar for more documentation.

To add something to Heiko's answer about the "overflow menu" on the action bar, this only happens if you have items set as ifRoom and there is no room for them to be displayed. On the overflow menu they only appear with a title and no icon.
On Android 4.0, the overflow menu ("3 dot spinner") is only shown on devices that don't have the physical "menu" button. You can test this on an ADV setting the option Hardware Back/Home keys option to "no".

Related

ActionBar item does not shown in Option Menu

I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.

Menu items showAsAction="never" are completely gone

I have this menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_preferences"
android:showAsAction="ifRoom"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_connect"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_goto"
android:showAsAction="never"
android:title="#string/action_connect"/>
<item
android:id="#+id/action_upgrade"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="never"
android:title="#string/action_upgrade"/>
</menu>
With this Activity code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return super.onCreateOptionsMenu(menu);
}
And only the action_settings action button appears. I would expect the other two to be available through the "three-dotted" menu, or the device's menu button. On the LG Optimus L3, the three-dotted menu does not appear and the device's menu button does nothing. On my Galaxy Nexus the menu does appear.
I want these other settings in a seperate menu because I don't want them to be tapped by accident. According to the documentation (emphasis mine):
The action bar provides users access to the most important action items relating to the app's current context.
If I cannot put it in the ActionBar menu, where to put the less-important action items?
It is because the LG has a hard menu button. When you press the menu button on the device, you should see the other two items.
Also, your placement of the settings icon is incorrect:
Even if there's room in the action bar, never make Settings an action
button. Always keep it in the action overflow and label it "Settings".
Place it below all other items except "Help".
http://developer.android.com/design/patterns/settings.html#flow-structure

Option menu same as like Whatsapp

I want Option menu in Action Bar same as like Whatsapp and many other applications.
Check Image shown as below :
Please help me to get this.
Android documentation says to you how to do that, here.
You need to create a new xml inside menu res folder.
An 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"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
</menu>
android:icon: Used to specify the icon to use
android:title: The name of the action (Example, if the button does a Search it could be "Search")
android:id: The ID of the menu, you will use it to know which menu has been clicked
Then to create it in your Activity override onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Replace R.menu.main_activity_actions with your menu name.
To apply the same effect like in yuor screen you should use android:showAsAction="never"
<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="never"
/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose"
android:showAsAction="never" />
</menu>
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
android:showAsAction is used to say how Android should show the icon; if you put ifRoom Android will try to add it as icon if there are space; if you use "never" it will add it in the overflow menu.
Official doc:
ifRoom Only place this item in the Action Bar if
there is room for it.
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
|.
never Never place this item in the Action Bar.
always Always place
this item in the Action 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 action bar.
collapseActionView The action view
associated with this action item (as declared by android:actionLayout
or android:actionViewClass) is collapsible.
Introduced in API Level 14.
If you use it in API < 14 it will just be ignored.

MenuItem showing up on a bottom bar

When I inflate the menu, some of the item are showing up in the bottom bar, while the others show up in the usual options menu of Android 2.
I tried with android:uiOptions="none" in the manifest, but then the bottom bar disappear and the options menu remain.
What I want to do is to add a menu item to the action bar!
Here is the menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_support"
android:icon="#drawable/icon_close"
android:showAsAction="always"
android:title="#string/menu_support"
android:visible="true"/>
<item
android:id="#+id/menu_feedback"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_feedback"/>
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_settings"/>
</menu>
and here is the onCreateMenuOptions:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu); return true;
}
The options menu is always visible because the new design is for devices to not have a menu button.
I would guess that you have setDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false). Then when you set android:uiOptions to none, that would cause your Actionbar to go away, but keeps the options menu icon. You need to keep that as splitActionBarWhenNarrow if you're disabling the top Actionbar.
Finally to get to the crux of your question. You have one item that is set as android:showAsAction=always. That option will always show up as an icon on the action bar. The other two are set to never. They will always show up in the options menu.
never = show as an action icon on the action bar.
ifRoom = show as an action icon on the action bar if there is room on the action bar, but put in the options menu when there isn't room.
always = always shown in the option menu.
|withText = adds the hint to show the action's title if there is room on the action bar.
The rule of thumb is to only set two items as always shown as actions so that the Actionbar does not get crowded on small devices. On larger devices your ifRoom options will be displayed as Actionbar items rather than option menu items.

why I do not see my Icon in Android Option Menu?

I have the following code to generate a Menu Item:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu1"
android:icon="#drawable/ic_action1"
android:title="#string/foo" />
</menu>
I see the Item in the menu, but I can not see the icon. Whats wrong?
I dont want to use it as the following:
android:showAsAction="ifRoom|withText"
then I see the icon, but I dont want it to be in the Action Bar
When you are using the ActionBar / Holo theme, you won't see any menu item icons in the overflow menus, sorry.
I think you will get the old menu back when you apply a pre-Holo theme to your activity!
try something like this
onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
menu.getItem(R.id.your_item_id).setIcon(R.drawable.your_drawable_id);
}

Categories

Resources