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?
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>
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>
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.
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
Yet, I believe both should have the same result.
I hope it help you.
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>
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>