Checks not showing in Options Menu - android

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"

Related

Options Menu shows slowly

In my Android Application I want to have a options menu in the toolbar and the functionality works just fine. However it takes a considerably long time (about one second) until the menu show after the 3 dots in the toolbar are pressed.
At first i thought, that it might be some problem specific to my app, but I created a new project from the Android Studio template and the problem still persists. (Though it feels a little bit faster than in my application)
The menu is created as documented in the docs:
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/manageBTConnection"
android:icon="#drawable/ic_bluetooth_white"
android:orderInCategory="100"
android:title="#string/connect"
app:showAsAction="ifRoom" />
<item
android:id="#+id/backgroundService"
android:title="Start background service" />
<item
android:id="#+id/sendToBackend"
android:title="Send to Backend" />
</menu>
MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
Compared to other Android apps it takes a lot longer than usual and feels clunky.
Is there any way to speed up the creation/showing of the dropdown menu?
Many thanks!
I'm experiencing the same thing. Here I've profiled what happens when I tap to open the menu. Notice that it takes 900 to 1000 milliseconds for the menu to appear.

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.

ActionBarSherlock menu item showing up on tablet not on phone

I am using ActionBarSherlock to create a menu (with the three dots) at the top and then have a few items as a submenu. The menu is showing up on tablets, but not on a four inch phone. The app can run on sdkVersion 9-17.
My menu.xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_weather"
android:title="#string/weather"
android:icon="#drawable/weather_tab"/>
<item
android:id="#+id/menu_emergency"
android:title="#string/emergency_nums" />
<item
android:id="#+id/menu_suggest"
android:title="#string/friend" />
<item
android:id="#+id/menu_support"
android:title="#string/faq" />
<item
android:id="#+id/menu_about"
android:title="#string/about" />
</menu>
And I have a menu inflate of:
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Thanks in advance for any help.
In your res/menu/menu.xml, your item can have a android:showAsAction parameter, which defines when to show them. You can then set a bunch of parameters that will help you displaying them (or not) more easily on phones and tablets. If you want to always show the menu item, just set this parameter to "always".
More here :
http://developer.android.com/guide/topics/resources/menu-resource.html
Try
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I kept searching and this is what finally worked for me: https://stackoverflow.com/a/11438245/2291915
I hope this can help someone else. I hadn't realized because I was using this on an emulator it was really about whether there was hard or soft buttons.

Android: How to include a menu xml inside another menu xml?

Simple question.
I have my menu of child items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/fp_pitcher"
android:title="Pitcher">
</item>
<item
android:id="#+id/fp_catcher"
android:title="Catcher">
</item>
<!-- SNIP --->
</menu>
And later I would want to include it as a submenu of this menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/teameditor_remove"
android:title="Remove Player from Team">
</item>
<item
android:id="#+id/teameditor_assignbattingposition"
android:title="Assign Batting Position">
</item>
<item
android:id="#+id/teameditor_assignfieldingposition"
android:title="Assign Feilding Position">
<!-- I want to include the submenu here-->
</item>
</menu>
The question here kind of answered this - I'm not sure how to inflate the submenu.
I'm thinking that you inflate it in the onContextItemSelected method - but inflate requires a menu object, which isn't passed into onContextItemSelected.
It's sadly not possible in plain XML, but there's a nice way without using manual Menu.add* methods: here's how you can obtain a Menu instance to include/inflate the other file into:
inflater.inflate(R.menu.player, menu);
MenuItem fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu()); // needs <menu />
You can put the above code to any of the following using the specified inflater:
Activity.onCreateContextMenu(menu, v, menuInfo): getMenuInflater()
Fragment.onCreateContextMenu(menu, v, menuInfo): getActivity().getMenuInflater()
Activity.onCreateOptionsMenu(menu): getMenuInflater()
Fragment.onCreateOptionsMenu(menu, inflater): inflater
menu/player.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/teameditor_remove"
android:title="Remove Player from Team"
/>
<item android:id="#+id/teameditor_assignbattingposition"
android:title="Assign Batting Position"
/>
<item android:id="#+id/teameditor_assignfieldingposition"
android:title="Assign Feilding Position">
<menu><!-- include: positions.xml --></menu>
</item>
</menu>
The empty <menu /> placeholder is very important, without that getSubMenu() will be null!
menu/positions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/fp_pitcher"
android:title="Pitcher"
/>
<item android:id="#+id/fp_catcher"
android:title="Catcher"
/>
<!-- SNIP --->
</menu>
Note on your onContextItemSelected idea
I'm thinking that you inflate it in the onContextItemSelected method [...]
I think it's too late if you're in onContextItemSelected, since you're already handling the event which would lead to showing you're submenu... which is not inflated yet. You could try the same inflate into getSubMenu(), but I'm not sure that it'll show up. It's best to create the menu where it's supposed to be created.
Note on including the same submenu multiple times in the same menu
Untested If you need to inflate the same positions.xml into teameditor_assignbattingposition as well you'll have some problems in onOptionsItemSelected/onContextItemSelected.
One way to work around it is to convert the findItem variable to a field and save the reference to both
this.fp_menu = menu.findItem(R.id.teameditor_assignfieldingposition);
inflater.inflate(R.menu.positions, fp_menu.getSubMenu());
this.bp_menu = menu.findItem(R.id.teameditor_assignbattingposition);
inflater.inflate(R.menu.positions, bp_menu.getSubMenu());
and then in on*ItemSelected:
switch (item.getItemId()) {
case R.id.fp_pitcher:
if (item == fp_menu.findItem(R.id.fp_pitcher)) {
// selected inside teameditor_assignfieldingposition
} else if (item == bp_menu.findItem(R.id.fp_picther)) {
// selected inside teameditor_assignbattingposition
} else {
throw new ImLostInMenusException();
}
return true;
}
return super.on*ItemSelected();
It's not pretty, but if you you need to do it without copying the XML content over (which would work easily). When you inflate the second menu you can also do a menu.findItem(R.id.teameditor_assignfieldingposition).getSubMenu().add(...) for each of the items you want to add. If you have the strings ("Pitcher" and "Catcher") in a String array resource you could iterate over that array to add the same items as in the original. Alternatively, you would probably need to parse the other menu's XML, you can cheat that by just inflating it I guess, and then using it's size() and getItem(int).
In fact, you could just inflate the first menu into a Menu and then use size() and getItem(int) to get the MenuItems out of it. Then, for each item you can do add(menuItem.getGroupId(), menuItem.getItemId(), menuItem.getOrder(), menuItem.getTitle()) on the getSubMenu() of the second menu's findItem(R.id.teameditor_assignfieldingposition). That should add all the items of the first menu as a submenu of that item. This means you are inflating two XML files, but it's kind of unavoidable if you want to use separate XML files, seeing as there isn't an <include> for menu XML files. I would probably inflate the second menu normally (in the onCreateOptionsMenu(...)) and then add the first menu as a submenu in the onPrepareOptionsMenu(...) (it's given the menu you created in onCreateOptionsMenu(...)). I think you could do it all in onCreateOptionsMenu(...), but I believe it's better practice to make modifications to the menu in onPrepareOptionsMenu(...).
I think the second way is the best solution I can find, I'm leaving the first option as an alternative just in case.

Set four menu items in single row

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.

Categories

Resources