I have the following menu ressource file (generated with androids Navigation Drawer Activity) and customized by me:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<item android:title="Foo">
<menu>
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</menu>
</item>
Setting the first level items works as expected:
navigationView.setCheckedItem(R.id.nav_home);
But setting a child menu item as checked, doesn't do anything.
navigationView.setCheckedItem(R.id.nav_about);
Any idea?
The problem was the generated layout by Android Studio. To check child items, I had to adjust the layout like this:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<item android:title="Foo">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
</menu>
</item>
Its because they are not under group tag , and they dont have property of android:checkableBehavior="single" .
Modify you code as
<item android:title="Foo">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
</menu>
Hope this helps.
If you are trying to select an Item of the Navigation drawer in the onCreate Method or anywhere else in your application, you can use the code below:
navigationView.getMenu().getItem(0).setChecked(true);
The getItem(int index) method gets the MenuItem then you can call the setChecked(true);on that MenuItem, all you are left to do is to find out which element index does the default have, and replace the 0 with that index.
You can select(highlight) the item by calling
onNavigationItemSelected(navigationView.getMenu().getItem(0));
You can refer to this as well :
NavigationView with DrawerLayout setCheckedItem not working
Also you can add the menu as:
<group
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/nav_users"
android:icon="#drawable/ic_menu_camera" />
<item
android:id="#+id/nav_following"
android:icon="#drawable/ic_people_black_24dp" />
</group>
<group
android:checkableBehavior="single">
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_exit_to_app_black_24dp" />
<item
android:id="#+id/nav_licences"
android:icon="#drawable/ic_assignment_black_24dp" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_info_black_24dp" />
</group>
Try this solution...
Use two line code, one is for checked Item of navigation drawer and second is for selection of item of sub-menu.
navigationView.setCheckedItem(R.id.nav_about);
onNavigationItemSelected(navigationView.getMenu().getItem(3).getSubMenu().getItem(2));
If you are creating your menu programatically you will have to do something similar to #Abdul Kawee's answer
Menu.setGroupCheckable(...)
Inside my NavigationView menu I have these items. There are some items with heading which is having some issues I think because when I used android:checkableBehavior it's not affected or doesn't show that it's selected or being highlighted. The items highlighted are only the nav1, nav2, nav3 items on the menu for the Drawer Layout. What I wanted to achieve is when the user clicks on the items with some headings on it like the nav4, or nav5 item It should get highlighted when selected.
<group android:checkableBehavior="single">
<item android:id="#+id/nav_1" android:icon="#drawable/ic_1"
android:title="#string/nav1" />
<item android:id="#+id/nav_2" android:icon="#drawable/ic_2"
android:title="#string/nav2" />
<item android:id="#+id/nav_3" android:icon="#drawable/ic_3"
android:title="#string/nav3" />
<item android:title="#string/heading1">
<menu >
<item android:id="#+id/nav_4" android:icon="#drawable/ic_4"
android:title="#string/nav4"/>
<item android:id="#+id/nav_5" android:icon="#drawable/ic_5"
android:title="#string/nav5" />
</menu>
</item>
</group>
if you want nav4 and nav5 to get highlighted too change to this:
<group android:checkableBehavior="single">
<item android:id="#+id/nav_1" android:icon="#drawable/ic_1"
android:title="#string/nav1" />
<item android:id="#+id/nav_2" android:icon="#drawable/ic_2"
android:title="#string/nav2" />
<item android:id="#+id/nav_3" android:icon="#drawable/ic_3"
android:title="#string/nav3" />
<item android:title="#string/heading1">
<menu >
<group android:checkableBehavior="single">
<item android:id="#+id/nav_4" android:icon="#drawable/ic_4"
android:title="#string/nav4"/>
<item android:id="#+id/nav_5" android:icon="#drawable/ic_5"
android:title="#string/nav5" />
</group>
</menu>
</item>
</group>
I have a problem in my action bar. I'm trying to add a checkbox on a menu item in ActionBar, but it does not work. It only shows the title of the checkbox.
This is my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.tracking.bus.maps.SingleViewMapsActivity" >
<item
android:id="#+id/actionbar_alarm"
android:title="#string/actionbar_alarm"
app:actionViewClass="android.widget.checkbox"
app:showAsAction="ifRoom"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Checkable items appear only in submenus or context menus. -->
<!-- Carefully look at the attribute name checkableBehavior on groups, but
the attribute name checkable on items. The checkableBehavior encompasses
the number of items that will be checkable within that group. -->
<item android:title="None">
<menu>
<!-- The none checkableBehavior is default, but we explicitly show it here. -->
<group
android:id="#+id/noncheckable_group"
android:checkableBehavior="none">
<!-- Notice how these items inherit from the group. -->
<item
android:id="#+id/noncheckable_item_1"
android:title="#string/item_1" />
<item
android:id="#+id/noncheckable_item_2"
android:title="#string/item_2" />
<item
android:id="#+id/noncheckable_item_3"
android:title="#string/item_3" />
</group>
</menu>
</item>
<item android:title="All">
<menu>
<group
android:id="#+id/checkable_group"
android:checkableBehavior="all">
<!-- Notice how these items inherit from the group. -->
<item
android:id="#+id/checkable_item_1"
android:title="#string/item_1" />
<item
android:id="#+id/checkable_item_2"
android:checked="true"
android:title="#string/item_2" />
<item
android:id="#+id/checkable_item_3"
android:checked="true"
android:title="#string/item_3" />
</group>
</menu>
</item>
<item android:title="Single">
<menu>
<group
android:id="#+id/exclusive_checkable_group"
android:checkableBehavior="single">
<!-- Notice how these items inherit from the group. -->
<item
android:id="#+id/exclusive_checkable_item_1"
android:title="#string/item_1" />
<item
android:id="#+id/exclusive_checkable_item_2"
android:title="#string/item_2" />
<item
android:id="#+id/exclusive_checkable_item_3"
android:checked="true"
android:title="#string/item_3" />
</group>
</menu>
</item>
<item android:title="All without group">
<menu>
<!-- Notice how these items have each set. -->
<item
android:id="#+id/nongroup_checkable_item_1"
android:checkable="true"
android:title="#string/item_1" />
<item
android:id="#+id/nongroup_checkable_item_2"
android:checkable="true"
android:checked="true"
android:title="#string/item_2" />
<item
android:id="#+id/nongroup_checkable_item_3"
android:checkable="true"
android:checked="true"
android:title="#string/item_3" />
</menu>
</item>
</menu>
You have to use the following attributes (depending on item):
For check boxes: android:checkableBehavior="all"
For radio buttons: android:checkableBehavior="single"
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>
i am trying to make my OnOptionMenu looks like this:
Any ideas on how to make it close to it??
Edit:
here is what i did:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:title="ajouter"
android:icon="#drawable/ic_action_plus"
android:showAsAction="always">
</item>
<item
android:id="#+id/item2"
android:title="rechercher"
android:icon="#drawable/ic_action_loupe"
android:showAsAction="always">
</item>
<item
android:id="#+id/item3"
android:title="editer"
android:icon="#drawable/ic_action_crayon"
android:showAsAction="ifRoom">
</item>
<item
android:id="#+id/item4"
android:title="supprimer"
android:icon="#drawable/ic_action_poubelle"
android:showAsAction="ifRoom">
</item>
<item
android:id="#+id/item5"
android:title="à propos"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_poubelle">
</item>
<item
android:id="#+id/item6"
android:title="Quitter"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_poubelle">
</item>
</menu>
but it still looks the same, i still get a quiet classic menu.( just the item's title on a list)
here is how it looks:
http://img651.imageshack.us/img651/3007/resultax.th.png
by the way do i have to inflate the actionbar menu and the menu on the bottom with two different inflaters??
any additional ideas??
Create resource file in res/menu and inflate it in onCreateOptionsMenu() Activity-method
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:title="Help"
android:icon="#drawable/ic_1/>
<item android:id="#+id/keyboard"
android:title="Keyboard"
android:icon="#drawable/ic_2" />
<item android:id="#+id/exit"
android:title="Exit"
android:icon="#drawable/ic_3/>
</menu>
It is very easy, you can do this in xml file itself
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/Non_Veg"
android:title="Non Veg"
android:icon="#drawable/hamburger">
<menu>
<item
android:id="#+id/Chicken"
android:title="Chicken"/>
<item
android:id="#+id/Butter_Chicken"
android:title="Butter Chicken" />
<item
android:id="#+id/Mutton"
android:title="Mutton" />
<item
android:id="#+id/Beef"
android:title="Beef" />
<item
android:id="#+id/Ham"
android:title="Ham" />
</menu>
</item>
</menu>
Here in item tag you can directly use the android:icon property to set the icon of the menu, hope I helped.