Navigation drawer menu item only clickable ubtitle - android

I'm trying to my menu but I have a problem, I would like to habe smt like that
that's my code
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Subtitle 1">
<menu>
<group android:id="#+id/group1">
<item
android:title="Item 1 "/>
<item
android:title="Item 2"/>
</group>
</menu>
</item>
<item android:title="Subtitle 2">
<menu>
<group>
<item ></item>
</group>
</menu>
</item>
<item android:title="Subtitle 3">
<menu>
<group>
<item
android:title="Item 1"/>
<item
android:title="Item 2"/>
</group>
</menu>
</item>
</menu>
I don't know exactly how to handle it, when I run this code I got that, I shouldn't have the empty space and subtitle 2 has to be clickable
Someone can help ?
Thx

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!!

NavigationView:setCheckedItem not working for child item

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(...)

Determining the SubMenu of a MenuItem

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.

Android design support navigation drawer selecting multiple items

I want to know if there is a way to tell the design support navigation drawer to only mark one item as selected, when I have sub-items which contain more items.
Code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_1"
android:icon="#drawable/ic_icon"
android:title="Item 1"
android:checked="true"/>
<item
android:id="#+id/nav_2"
android:icon="#drawable/ic_icon"
android:title="Item 2" />
</group>
<item android:title="Subtitle 1">
<menu>
<item
android:id="#+id/nav_2"
android:icon="#drawable/ic_icon"
android:title="Item 3" />
<item
android:id="#+id/nav_4"
android:icon="#drawable/ic_icon"
android:title="Item 4" />
<item
android:id="#+id/nav_5"
android:icon="#drawable/ic_icon"
android:title="Item 5" />
</menu>
</item>
</menu>
I have Item 1 selected by default and it is the first fragment that appears when launching the app. When I press on an item in a subtitle, Item 5 for example, Item 1 still has the darker background and is colored in the accent color, while Item 5 is only colored in the accent color without the dark background. How can I archive that only one items gets marked at one time?
the group needs to be around all items like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_1"
android:icon="#drawable/ic_icon"
android:title="Item 1"
android:checked="true"/>
<item
android:id="#+id/nav_2"
android:icon="#drawable/ic_icon"
android:title="Item 2" />
<item android:title="Subtitle 1">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_2"
android:icon="#drawable/ic_icon"
android:title="Item 3" />
<item
android:id="#+id/nav_4"
android:icon="#drawable/ic_icon"
android:title="Item 4" />
<item
android:id="#+id/nav_5"
android:icon="#drawable/ic_icon"
android:title="Item 5" />
</group>
</menu>
</item>
</group>
</menu>

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.

Categories

Resources