I have a menu(that comes when the user clicks default menu button) in which i have four items. So iam using following code to display menu. Problem is four items come in two rows(2x2). Is it possible to display in a single row.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.editshare, menu);
return true;
}
menu/editshare.xml
<item android:id="#+id/item1"
android:icon="#drawable/icon"
android:title="item1" />
<item android:id="#+id/item2"
android:icon="#drawable/icon"
android:title="item" />
<item android:id="#+id/item3"
android:icon="#drawable/icon"
android:title="item3" />
<item android:id="#+id/item4"
android:icon="#drawable/icon"
android:title="item4" />
</menu>
I guess this is what you are looking for:
http://androidworkz.com/2011/02/04/custom-menu-bar-tabs-how-to-hook-the-menu-button-to-showhide-a-custom-tab-bar/
i think this is not possible with default menu, But you can create one custom menu type and show it when menu button is pressed. and hide when select any option.
You can say it is one transparent slider which pop up when you click menu button. And hide it when option selected or again menu is pressed...
Its default behavior of android menu items, and it can not be altered.
It's called common usability :P You have to think that Android is very smart so it will display in 2 rows on phones with a lower res/density and in a single row on a phone (or orientation) with bigger res/density :)
You can't display four menus in a single row in android. This is controlled by the system.
You can use four buttons with layout_weight= "1.0" but this will use your UI space.
Related
I have 4 items defined in xml as secondary then later I create one more in the code defined as container. I'm trying to get the one created in code (share) to be first, it's not working. It is always added to the end of the menu. I was under the impression that the menu category is what decided the action bar order. What am I missing?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_unlock"
android:menuCategory="secondary"
android:title="Unlock"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_share"
android:menuCategory="secondary"
android:title="Share App"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_follow"
android:menuCategory="secondary"
android:title="Follow"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_review"
android:menuCategory="secondary"
android:title="Review"
android:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
actions = menu;
// Inflate the menu items for use in the action bar
getMenuInflater().inflate(R.layout.menu, menu);
return super.onCreateOptionsMenu(menu);
}
/*
* Set whether share buttons should be available.
*/
public void setShareable (boolean shareable)
{
if (!shareable)
return;
// Build share button
MenuItem item = actions.add (Menu.NONE, Menu.NONE, Menu.CATEGORY_CONTAINER, "Share");
item.setShowAsAction (MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
The correct usage of menu categories is to use FIRST instead of CONTAINER for the menu item that needs to get highest priority, and the rest as SECONDARY as used in the question.
An alternate way to order the menu items is through the android:orderInCategory attribute. Set the first preexisting menu item to, say, order 10 (even 2 would work in your case), and then you can add up to 9 other menu items before it, dynamically, by setting this attribute in the code (menu.add(Menu.NONE,Menu.NONE, order,R.string.my_menu_item). The order variable here is a little tricky, since it's an int that represents both the group association and the order within that group: see here.
I'm having a problem when I try to set one item in my actionbar as always visible and 4 more icons as dropdown items with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_movies"
android:icon="#drawable/action_search"
android:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/movies"
android:icon="#drawable/action_video"
android:showAsAction="collapseActionView"
android:title="Movies"/>
<item
android:id="#+id/theaters"
android:icon="#drawable/action_location_map"
android:showAsAction="collapseActionView"
android:title="Theaters"/>
<item
android:id="#+id/preferences"
android:icon="#drawable/action_settings"
android:showAsAction="collapseActionView"
android:title="Preferences"/>
<item
android:id="#+id/contact"
android:icon="#drawable/action_about"
android:showAsAction="collapseActionView"
android:title="Contact"/>
</menu>
The result is just the first item showing and the rest are not visible, not even as a dropdown. This is using ActionBarSherlock and a 2.3 Android device.
The question is, how can I get the icons to follow this layout:
EDIT:
The problem I had was because when you are using the actionbar with a device that has a "menu" hardware button the 3-dot dropdown does not shows off, the 4 other items are only displayed if you press the menu hardware button. Does anyone knows if this behaviour can be modified?
Hmmm, maybe I misunderstood, but if you wish to places those remaining four items into the overflow action menu (the 3-dot icon) then using android:showAsAction="never" instead of "collapseActionView" should do it.
...Tried a couple ways, but this did the trick:
Force overflow menu in ABS
I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)
The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("More");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sub.getItem().setIcon(R.drawable.ic_menu);
getSupportMenuInflater().inflate(R.menu.activity_main, sub);
return true;
}
Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.
It works!
On a Galaxy S3, I have an issue where a button in the ActionBar is configured as showAsAction="always" and being shown on both the hardware overflow menu and the Action buttons. I would like it to not be shown on the hardware overflow menu and only on the Action buttons. I can disable the menuitem in the onCreateOptionsMenu but it will hide the button on both places.
Something to note: if I force the "3 dots" Action Overflow menu to show, the refresh button gets properly hidden from the hardware overflow menu but still doesn't get hidden from the hardware overflow menu.
Something else to note: if I call menu.size() in either onCreateOptionsMenu or onPrepareOptionsMenu, it doesn't reflect the extra button. For example, I have four buttons and the first button is being shown in both the Action buttons and the overflow menu. menu.size() still returns 4 and doesn't seem to realize that it is showing an extra button.
I can't post a screenshot because this is an app for a client but here is my actionbar.xml file. The refresh button shows in both the overflow and the action bar at the top.
actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refreshmenuitem"
android:icon="#drawable/refreshicon"
android:title="Refresh"
android:showAsAction="ifRoom"
android:visible="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<item android:id="#+id/helpbutton"
android:title="Help"
android:showAsAction="collapseActionView"
android:icon="#drawable/ic_action_helpbutton" />
<item android:id="#+id/settingbutton"
android:title="Settings"
android:icon="#drawable/ic_action_settingbutton"
android:showAsAction="collapseActionView" />
<item android:id="#+id/importbutton"
android:title="Import file"
android:icon="#drawable/ic_action_importbutton"
android:showAsAction="collapseActionView" />
</menu>
So I figured it out but it is kind of a hack. So if you have a phone such as the Samsung Galaxy S3 that has a hardware menu button, the onCreateOptionsMenu function actually gets called twice. Once for when the Activity gets loaded to load any menu items that should display in the top right and another time when the user presses the hardware menu button. All I did was create a variable called _firstTimeOnCreateOptionsMenu that is set to false after the first time it is onCreateOptionsMenu method:
public boolean onCreateOptionsMenu(Menu menu)
{
// Populates the actionbar/Menu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
boolean hardware = false;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
hardware = ViewConfiguration.get(context).hasPermanentMenuKey();
}
MenuItem b1 = menu.findItem(R.id.refreshmenuitem);
if(!hardware || _firstTimeOnCreateOptionsMenu)
{
b1.setVisible(true);
_firstTimeOnCreateOptionsMenu = false;
}
else
{
b1.setVisible(false);
_firstTimeOnCreateOptionsMenu = true;
}
}
Hopefully this helps anyone else who is having this issue.
Is your targetSdkVersion set to 14?
http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html
I'd be a bit surprised if this was the issue, since you'd expect unknown XML to just be ignored, but according to the documentation <item> does not support android:layout_width and android:layout_height.
I have 3 menu icons on the menu bar, but everytime it only shows 2 icons, the last one is in nowhere.
my questions are:
1. there is enough space for 3 icons, why only 2 are shown?
2. if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
Below is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom" />
<item android:id="#+id/add_homework"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom" />
<item android:id="#+id/set_groupid"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="ifRoom" />
</menu>
and this snippet is in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I have 3 menu icons on the menu bar
For the purposes of this answer, I am assuming that by "menu bar" you are referring to the action bar.
but everytime it only shows 2 icons, the last one is in nowhere
The last one is available by pressing the MENU button, for devices (or emulators) that have an off-screen MENU button.
there is enough space for 3 icons, why only 2 are shown?
Presumably because Android disagrees with your assessment of whether or not there is enough space for 3 icons.
if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
I have no idea why you think forcing the 2nd icon -- which, by your admission, fits -- into the overflow menu would be a good idea. The 3rd menu item is in the overflow menu, which is accessed via the MENU button on devices that have one or a three-vertical-dots button on the action bar for devices that lack a MENU button.
I encountered this same issue and resolved it by doing two things, although I think the main reason is the second item. I was using the Android Asset Studio to create icons for my Actionbar menu.
In Android Asset Studio, I set the icon to "trim".
In my menu/activity_main.xml definition file (or whatever you call your menu definition file) I defined my menu icons with android:showAsAction="always"
It looked something like this:
<item android:id="#+id/menu_test"
android:icon="#drawable/ic_menu_test"
android:title="#string/menu_test"
android:showAsAction="always" />
Sometimes when doing some so very simple, you miss something big. I must be missing something huge because I am getting nowhere fast (and an hour of sifting through the web has revealed nothing).
I want to have a menu with items with checkmarks in group--just like a simple RadioGroup layout. I get the menu, but no checkmarks of any kind.
Here's the res/menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/item1"
android:title="item1"
/>
<item
android:id="#+id/item2"
android:title="item2"
/>
<item
android:id="#+id/item3"
android:title="item3"
android:checked="true"
/>
</group>
</menu>
And of course, here's the relevant methods in my Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
This is about as simple as I can make it--any ideas on what I'm missing?
I think you try to create Options Menu (it's displayed by click menu button) and according to this
Note: Menu items in the Icon Menu (from the Options Menu) cannot
display a checkbox or radio button. If you choose to make items in the
Icon Menu checkable, you must manually indicate the checked state by
swapping the icon and/or text each time the state changes.
you can't add group menu in OptionsMenu. so i think you shoud use Context Menu or Submenu.
Take a look at this article
Here's the work-around:
In your strings.xml file you can embed a unicode checkmark. There are two to choose from. For this project I prefer the friendlier check of \u2714. You then swap a string with the check-mark for a string without it via onPrepareOptionsMenu() as appropriate.
Here's the xml code two strings, one with and one without checkmarks:
<string name="opp_random">unchecked</string>
<string name="opp_random_check">\u2714 checked</string>
Happy coding!
How about
android:checkable="true"