Android: Pop up Menu on Menu button pressed - android

I want to have a normal menu button pop up a second menu.
I don't want a listview of options to display, I want a second, standard menu to pop up when I press a button on the menu.
Using openOptionsMenu(); works when you use onCreateOptionsMenu
but it does not work when you use: onPrepareOptionsMenu
Since I want to change the options on the menu dynamically, I need to use onPrepareOptionsMenu.
Thanks

Use a menu group for any menu item that doesn't have an action itself but opens up a second menu. Here's an example of one I did.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:titleCondensed="Cats" android:title="Cats" android:id="#+id/cats">
<menu>
<group android:id="#+id/catsGroup">
<item android:titleCondensed="Lolz Cats" android:title="Lolz Cats" android:id="#+id/twod"></item>
<item android:titleCondensed="Ugly Cats" android:title="Ugly Cats" android:id="#+id/ref"></item>
<item android:titleCondensed="Dumb Cats" android:title="Dumb Cats" android:id="#+id/vr"></item>
</group>
</menu>
</item>
<item android:titleCondensed="Dogs" android:title="Dogs" android:id="#+id/dogs">
<menu>
<group android:id="#+id/dogsGroup">
<item android:titleCondensed="Awesome Dog" android:title="Awesome Dog" android:id="#+id/pan"></item>
<item android:titleCondensed="Under Dog" android:title="Under Dog" android:id="#+id/zoom"></item>
<item android:titleCondensed="Snoopy" android:title="Snoopy" android:id="#+id/contrast"></item>
<item android:titleCondensed="Scooby Doo" android:title="Scooby Doo" android:id="#+id/page"></item>
<item android:titleCondensed="Pluto" android:title="Pluto" android:id="#+id/rotate"></item>
</group>
</menu>
</item>
</menu>
The first menu items that will show are "Cats" and "Dogs". When you select on of these, their sub items will be shown. Hope this helps.

Related

Update font style for Android MenuItem

I got a problem with the android code. I have a Menu with a SubMenu and I want to set the font style to bold for (Sub)MenuItem if it was clicked by the user. So the menu acts like a filter list, where you can select only one filter at the time and the selected MenuItem become bold. I have to do this programatically, benause the categories were generated/added dynamically.
The menu structure looks like this
...
<menu>
<item android:id="#+id/menu_sort_alphabetically" android:title="by name"></item>
<item android:id="#+id/menu_sort_value" android:title="by value"></item>
<item android:id="#+id/menu_sort_category" android:title="by category">
<menu android:id="#+id/menu_category"
android:checkableBehavior="single">
<item android:title="Category 1"></item>
<item android:title="Category 2"></item>
...
</menu>
</item>
</menu>
...
I tried several ways, but haven't found any solution or idea. It would be nice if someone could help me.

How to always have 2 actionbar items + overflow

I am having 5 items in the menu, and I only want to show 2 at the time.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_start"
android:icon="#drawable/ic_action_start"
android:orderInCategory="93"
android:showAsAction="ifRoom|withText"
android:title="#string/menu_start"/>
<item
android:id="#+id/menu_filter"
android:icon="#drawable/filter_icon"
android:orderInCategory="94"
android:showAsAction="ifRoom|withText"
android:title="#string/menu_filters"/>
<item
android:id="#+id/menu_refresh"
android:icon="#drawable/ic_menu_refresh"
android:orderInCategory="95"
android:showAsAction="never"
android:title="#string/menu_refresh"/>
<item
android:id="#+id/menu_delete_all"
android:icon="#drawable/ic_menu_delete"
android:orderInCategory="96"
android:showAsAction="never"
android:title="#string/menu_delete_all"/>
<item
android:id="#+id/menu_show_hidden"
android:orderInCategory="97"
android:showAsAction="never"
android:icon="#drawable/ic_menu_show_hidden"
android:title="#string/menu_show_hidden"/>
</menu>
When I test this on Galaxy Nexus, or any device that doesnt have menu button, it creates an overflow menu, however it does not add to be the third one as I would liked, but it removes my second icon and replaces it.
How do I have to setup the actionbar, to have 2 icons on devices without menu button, and 2 + (overflow) on devices that have?
Thanks !

How can i add custom option menu icon Android

i want to add customs icons to my option menu, like that :
How can i do that with custom icons in my menu.xml ?
Or if you know the list of defaults icons, i am interested by that...
Here is my actual menu.xml code :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" >
</item>
<item android:id="#+id/item1" android:title="Accueil" android:titleCondensed="Accueil" android:icon="#android:drawable/ic_menu_home"></item>
</menu>
Thanks !
Inside of the item tag, use android:icon="#drawable/your_icon". That should work.

contextual action bar - getting rid of Select Action text

I've implemented a contextual action bar. The menu being loaded and inflated in onCreateActionMode is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/call_context_menu_title">
<item android:id="#+id/menu_answer_call"
android:title="#string/answerCall"
android:showAsAction="ifRoom" />
<group android:id="#+id/activeCallContextMenuItems">
<item android:id="#+id/menu_end_call"
android:title="#string/hangupCall"
android:showAsAction="ifRoom"/>
<group android:id="#+id/multipleActiveCallsContextMenuItems">
<item android:id="#+id/menu_transfer_call"
android:title="#string/transferCall"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_conference_call"
android:title="#string/conferenceCall"
android:showAsAction="ifRoom"/>
</group>
</group>
</menu>
These items are shown when I click on an item in a ListView. When it is active, besides listing my 4 options, it puts a "Select Action" text before the first menu item. Is there any way to get Android to not show the "Select Action" hint? The menu items have self explanatory text so it is redundant (and even confusing if the hint is being cut off in the middle... e.g. on my S3 it says "Select..." and then I have my menu items)

Android option menu - one menu item in a single row

I am trying to use option menus for my application . When I add 2 MenuItem it shown in a single row, but i need only one item in a row and other in next row. Please help me.
Thanks..
You cannot. The Android system handles how the options menu is laid out and there are no options to achieve what you want. You would have to make your own View, and then slide this up/down when the menu button is pressed.
tr this code
<item android:id="#+id/last_most_item"
android:orderInCategory="10"
android:title="#string/last_most_often" />
<item android:id="#+id/middle_most_item"
android:orderInCategory="7"
android:title="#string/middle_most_often" />
<item android:id="#+id/first_most_item"
android:orderInCategory="4"
android:title="#string/first_most_often" />
</group>
I'm not sure it's possible but try with the MenuInflater and a menu resource file.
In your menu resource file, try to embed each item in a separated <menu> element, something like this :
<menu>
<item>
<menu>
<item android:id="#+id/item1"
android:title="#string/item1" />
</menu>
</item>
<item>
<menu>
<item android:id="#+id/item2"
android:title="#string/item2" />
</menu>
</item>
</menu>
Maybe it will force the inflater to show the items in 2 separated lines, sorry I don't have the time to test it. If it's not working, replace the submenus with <group> elements and retest.

Categories

Resources