Determining the SubMenu of a MenuItem - android

I have a NavigationView in my app. Inside the menu for the NavView, I have two sub menus:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_group_one"
android:title="SubMenu A">
<menu>
<item
android:checkable="true"
android:title="One"/>
<item
android:checkable="true"
android:title="Two"/>
</menu>
</item>
<item
android:id="#+id/nav_group_two"
android:title="SubMenu B">
<menu>
<item
android:checkable="true"
android:title="Three"/>
<item
android:checkable="true"
android:title="Four"/>
</menu>
</item>
</menu>
In my activity's onNavigationItemSelected() I would like to be able to figure out which sub menu the selected item is in. How would I do this?

So far I haven't found an existing solution at this time, however I have found a workaround:
To solve this with minimal additional XML overhead, wrap each set of sub-menu items in a group, with an Id:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_group_one"
android:title="SubMenu A">
<menu>
<group id="#+id/nav_group_wrapper_one">
<item
android:checkable="true"
android:title="One"/>
<item
android:checkable="true"
android:title="Two"/>
</group>
</menu>
</item>
<item
android:id="#+id/nav_group_two"
android:title="SubMenu B">
<menu>
<group id="#+id/nav_group_wrapper_two">
<item
android:checkable="true"
android:title="Three"/>
<item
android:checkable="true"
android:title="Four"/>
</group>
</menu>
</item>
</menu>
This way, one can simply call item.getGroupId() to infer the sub-menu it is contained within.

Related

How to add title to group on PopupMenu, not submenu

I want to add the title of the group of items of the submenu. I don't want to use the submenus which are hidden and open on its click, I want to implement a title of the group of menu items. So now my menu looks like this:
But I want to implement it like this:
Now my XML seems like this, but it's using a nested submenus:
<?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"
>
<group
android:id="#+id/group_1"
android:checkableBehavior="single"
>
<item
android:id="#+id/apps_sort_title"
android:title="#string/apps_sort_title"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/apps_sort_date"
android:title="#string/apps_sort_date"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/apps_sort_size"
android:title="#string/apps_sort_size"
app:showAsAction="ifRoom"
/>
</group>
<item android:title="#string/title_order">
<menu>
<group
android:id="#+id/group_2"
android:checkableBehavior="single"
>
<item
android:id="#+id/apps_sort_asc"
android:title="#string/sort_asc"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/apps_sort_desc"
android:title="#string/sort_desc"
app:showAsAction="ifRoom"
/>
</group>
</menu>
</item>
</menu>
Thanks in advance!
Set the tittle on group, you have to implement your menu XML like this.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/gp1"
android:checkableBehavior="single"
android:visible="true">
<item android:title="Group Title">
<menu>
<item
android:id="#+id/group_item_one"
android:title="Item one"/>
<item
android:id="#+id/group_item_two"
android:title="Item two"/>
<item
android:id="#+id/group_item_three"
android:title="Item three"/>
</menu>
</item>
</group>
<group
android:id="#+id/gp2"
android:checkableBehavior="single">
<item
android:id="#+id/group_two_item_one"
android:title="Item one"/>
</group>
Finally found the solution I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method.
menu.setGroupVisible(R.id.gp1, false);
Happy coding!!

Only 4 Items in ActionBar in Android?

I have an ActionBar with 5 Items but app (no matter horizontally or vertically) shows only 4 Item icons. Additional Item is visible after clicking More (ellipsis) button. I turned of Title and Home and still app displays only 4 Items on the right of the screen.
How to extend default space in ActionBar for custom Items?
It might simply be the case that you haven't set the android:showAsAction attribute for the menu items, so by default it is hiding them once there is a certain number. Try setting android:showAsAction="always" for all of the items.
Oh, Yes menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="pl.waw.miechoo.gpsspeed2.MainActivity"
>
<item android:id="#+id/menu_information"
android:icon="#drawable/ic_action_about"
android:title="Info"
/>
<item
android:id="#+id/menu_screen_orientation"
android:icon="#drawable/synchronization"
android:menuCategory="container"
android:orderInCategory="0"
android:showAsAction="ifRoom"
android:title="#string/menu_screen_orientation_string"
android:titleCondensed="#string/menu_screen_orientation_condensed_string">
<menu>
<group
android:id="#+id/action_settings"
android:checkableBehavior="single"
android:enabled="true"
android:menuCategory="container"
android:visible="true" >
<item
android:id="#+id/menu_screen_orientation_landscape"
android:title="#string/menu_screen_orientation_landscape_string">
</item>
<item
android:id="#+id/menu_screen_orientation_portrait"
android:title="#string/menu_screen_orientation_portrait_string">
</item>
<item
android:id="#+id/menu_screen_orientation_auto"
android:title="#string/menu_screen_orientation_auto_string">
</item>
</group>
</menu>
</item>
<item
android:id="#+id/menu_meause_unit_type"
android:icon="#drawable/checklist"
android:menuCategory="container"
android:orderInCategory="1"
android:showAsAction="always"
android:title="#string/measureUnitType"
android:titleCondensed="#string/measureUnitTypeCondensed">
<menu>
<group
android:checkableBehavior="single"
android:enabled="true"
android:visible="true" >
<item
android:id="#+id/menu_mps"
android:title="#string/menu_mps_string"/>
<item
android:id="#+id/menu_kmph"
android:title="#string/menu_kmph_string"/>
<item
android:id="#+id/menu_mph"
android:title="#string/menu_mph_string"/>
<item
android:id="#+id/menu_fps"
android:title="#string/menu_fps_string"/>
<item
android:id="#+id/menu_knots"
android:title="#string/menu_knots_string"/>
<item
android:id="#+id/menu_mach"
android:title="#string/menu_mach_string"/>
<item
android:id="#+id/menu_c"
android:title="#string/menu_c_string"/>
</group>
</menu>
</item>
<item
android:id="#+id/menu_hud"
android:icon="#drawable/gear"
android:menuCategory="container"
android:orderInCategory="2"
android:showAsAction="always"
android:title="#string/menu_hud">
<menu>
<group
android:checkableBehavior="single"
android:enabled="true"
android:visible="true" >
<item
android:id="#+id/menu_hud_off"
android:title="#string/menu_hud_off">
</item>
<item
android:id="#+id/menu_hud_on"
android:title="#string/menu_hud_on">
</item>
</group>
</menu>
</item>
<item
android:id="#+id/menu_precision"
android:icon="#drawable/compass"
android:menuCategory="container"
android:orderInCategory="2"
android:showAsAction="always"
android:title="#string/menu_precission_string">
<menu>
<group
android:checkableBehavior="single"
android:enabled="true"
android:visible="true" >
<item
android:id="#+id/menu_prec_0"
android:title="0">
</item>
<item
android:id="#+id/menu_prec_1"
android:title="0,0">
</item>
<item
android:id="#+id/menu_prec_2"
android:title="0,00">
</item>
<item
android:id="#+id/menu_prec_3"
android:title="0,000">
</item>
<item
android:id="#+id/menu_prec_4"
android:title="0,0000">
</item>
<item
android:id="#+id/menu_prec_5"
android:title="0,00000">
</item>
<item
android:id="#+id/menu_prec_6"
android:title="0,000000">
</item>
</group>
</menu>
</item>

How to keep single checkableBehavior mode in drawer menu for NavigationView when we add section?

I try to implement a drawer with new component of material design : NavigationView.
It's work very well. When I select an item changes its color change well with android:checkableBehavior="single".
<group
android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/home"/>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
...
<item
android:id="#+id/drawer_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/settings"/>
</group>
The problem come when I try to use section in drawer. It's this case, I can't use android:checkableBehavior="single" and I lost the color change in the selection of an item.
<item
android:id="#+id/section"
android:title="#string/section_title">
<menu>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
<item
android:id="#+id/drawer_downloaded"
android:icon="#drawable/ic_file_download_black_24dp"
android:title="#string/downloaded"/>
</menu>
</item>
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/home"/>
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
...
<item
android:id="#+id/drawer_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/settings"/>
<item
android:id="#+id/section"
android:title="#string/section_title">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_favourite"
android:icon="#drawable/ic_favorite_black_24dp"
android:title="#string/favourite"/>
<item
android:id="#+id/drawer_downloaded"
android:icon="#drawable/ic_file_download_black_24dp"
android:title="#string/downloaded"/>
</group>
</menu>
</item>
</group>
</menu>
you can check this solution for details..
I am unable to set a submenu item as checked
As a workaround until the bug reported by Fondesa is fixed you can use this:
Menu Definition
<item android:checkable="true" ...>
Styling
<item android:state_selected="true" android:color="#color/error_color"/>
This will properly highly the menu item when selected.
Note that this will not address the requirement:
android:checkableBehavior="single"
You will have to handle that manually.

Android Remove Gap in Settings menu item

How could I remove icons gap in float action menu. I want to have settings without icons.
See images:
Here menu code
<item
android:id="#+id/menu_delete"
android:icon="#android:drawable/ic_menu_delete"
android:showAsAction="ifRoom"
android:title="Delete email"
android:titleCondensed="Delete">
</item>
<item
android:id="#+id/menu_more"
android:icon="#drawable/abc_ic_menu_moreoverflow_normal_holo_light"
android:title="Settings"
android:showAsAction="always">
<menu>
<item
android:id="#+id/submenu_readAllJobs"
android:title="#string/ReadAllJobsEmail"/>
<item
android:id="#+id/submenu_deleteAllJobs"
android:title="#string/DeleteAllJobsEmail"/>
</menu>
</item>
Resolved problem by changing menu code to following:
<item
android:id="#+id/menu_delete"
android:icon="#android:drawable/ic_menu_delete"
android:showAsAction="ifRoom"
android:title="Delete email"
android:titleCondensed="Delete">
</item>
<item
android:id="#+id/submenu_readAllJobs"
android:title="#string/ReadAllJobsEmail"
app:showAsAction="never"
/>
<item
android:id="#+id/submenu_deleteAllJobs"
android:title="#string/DeleteAllJobsEmail"
app:showAsAction="never"
/>
Result image:

How do I add a title to my menu group

I want to differentiate the groups by giving them a title or divider, but I can't find a title option for the group element.
Is there a way to add a title or divider?
<group android:id="#+id/menu_group_sort" >
<item
android:id="#+id/menu_sort_relevance"
android:showAsAction="never"
android:title="#string/sort_relevance"/>
<item
android:id="#+id/menu_sort_rating"
android:showAsAction="never"
android:title="#string/sort_rating"/>
</group>
I use this code for my project, take a look:
<item
android:title="Search Option"
android:id="#+id/menu_search_option"
android:showAsAction="ifRoom|withText">
<menu>
<group>
<item
android:id="#+id/menuSearchFilmEpisode"
android:title="Episode"/>
<item
android:id="#+id/menuSearchActor"
android:title="Actor"/>
<item
android:id="#+id/menuSearchDirector"
android:title="Director"/>
</group>
</menu>
</item>
And the result is that i have a group with text as icon on my action bar. Hope it helps.
Perhaps two years too late, but thought it might be useful for whoever stumbles upon this problem...
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group_one">
<item android:title="Group Title">
<menu>
<item
android:id="#+id/group_item_one"
android:title="Item one"/>
<item
android:id="#+id/group_item_two"
android:title="Item two"/>
<item
android:id="#+id/group_item_three"
android:title="Item three"/>
</menu>
</item>
</group>
<group
android:id="#+id/group_two">
<item
android:id="#+id/group_two_item_one"
android:title="Item one"/>
</group>
Although that might not be the best approach, I believe it might help you out.
What about you add extra disabled itens as "titles" inside the groups, also you could create a style for those itens so they might show the color you want.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item
android:title="menu1"
android:enabled="false"
android:checkable="false">
</item>
<item
android:title="opt1">
</item>
<item
android:title="opt2">
</item>
<item
android:title="opt3">
</item>
</group>
<group>
<item
android:title="menu2"
android:enabled="false"
android:checkable="false">
</item>
<item
android:title="opt4">
</item>
<item
android:title="opt5">
</item>
</group>
</menu>
You also can try something like that, using java.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
menu.addSubMenu(Menu.NONE, 1, 1, "SubMenu");
menu.add(1, 2, 2, "Item");
menu.add(1, 3, 3, "Item");
menu.addSubMenu(Menu.NONE, Menu.NONE, 4, "SubMenu");
menu.add(4, 5, 5, "Item");
menu.add(4, 6, 6, "Item");
menu.add(4, 7, 7, "Item");
return true;
}
Parameters
groupId
The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId
Unique item ID. Use NONE if you do not need a unique ID.
order
The order for the item. Use NONE if you do not care about the order. See getOrder().
title
The text to display for the item.
Yet, I believe both should have the same result.
I hope it help you.
As we all know, an item-node can has title, and an item-node can be the child of menu-node, further more a menu-node can be a child of item-node, and a group-node can be a child of a menu-node.
so all we need to do is keep the group inside the hierarchy of rootMenu-itemWithTitle-menu.
like this, is is used as a NavigationView menus:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity">`
<item android:title="#string/drawer_group_book">
<menu>
<group android:id="#+id/nav_group_book"
android:checkableBehavior="single">
<item
android:id="#+id/nav_book_in_stock"
android:icon="#drawable/vector_in_stock"
android:title="#string/drawer_menu_in_stock"/>
<item
android:id="#+id/nav_book_wish_list"
android:icon="#drawable/vector_wish"
android:title="#string/drawer_menu_wish_list"/>
<item
android:id="#+id/nav_book_lent_out"
android:icon="#drawable/vector_out"
android:title="#string/drawer_menu_lent_out"/>
<item
android:id="#+id/nav_book_borrowed"
android:icon="#drawable/vector_in"
android:title="#string/drawer_menu_borrowed"/>
</group>
</menu>
</item>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share"/>
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send"/>
</menu>
</item>
</menu>
Just give a unique id to each group. It will create a separator automatically.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group_items"
android:checkableBehavior="single">
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_profile"
android:title="#string/title_profile" />
<item
android:id="#+id/nav_notifications"
android:icon="#drawable/ic_notifcation"
android:title="#string/title_notifications" />
</group>
<group
android:id="#+id/group_settings"
android:checkableBehavior="single">
<item
android:id="#+id/nav_privacy_policy"
android:title="#string/privacy_policy" />
<item
android:id="#+id/nav_about_us"
android:title="#string/nav_about_us" />
</group>
This might be a little late, but for whom it might help, The below menu code adds title to a menu group and below it 2 checkable options to choose from.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/gp_hour_view"
android:checkableBehavior="single">
<item
android:checkable="false"
android:title="Display">
<menu>
<group
android:id="#+id/gp_hour_sel"
android:checkableBehavior="single">
<item
android:id="#+id/action_pinched_view"
android:checked="true"
android:orderInCategory="201"
android:title="Pinched"
app:showAsAction="never" />
<item
android:id="#+id/action_zoomed_view"
android:orderInCategory="201"
android:title="Zoomed"
app:showAsAction="never" />
</group>
</menu>
</item>
</group>
</menu>
This is my idea:
<menu>
<group>
<item android:title="grouptitle"/>
<item android:title="item1" android:id="#id+/item1"/>
</group>
</menu>
To have a title in each group within your menu, you can create a disabled item that acts as a title. This way it will not be clickable and will have a different color from the elements in your group. Also, in order to make the difference between selectable and non-selectable items more evident, you can add an icon only to the title item of the group.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group
android:id="#+id/group1"
android:checkableBehavior="single">
<item
android:title="Title 1"
android:enabled="false"
android:icon="#drawable/ic_star" />
<item
android:title="#string/menu_home" />
<item
android:title="#string/menu_home" />
<item
android:title="#string/menu_home" />
</group>
<group
android:id="#+id/group2"
android:checkableBehavior="single">
<item
android:title="Title 2"
android:enabled="false"
android:icon="#drawable/ic_star" />
<item
android:title="#string/menu_home" />
<item
android:title="#string/menu_home" />
<item
android:title="#string/menu_home" />
</group>
</menu>

Categories

Resources