Android drawer selection and scrolling with submenu - android

I have two problems with the drawer submenu (I use the drawer example generated with the android studio wizard)
First problem
I try to select de subitems, for example if I click on "Tools" the selecteItem "effect" its works fine, but when I try to click on "Share" or "Send" and the selectedItem "effect" is not works:
(This is crazy because is the Android example, I suppose that this is an official example and should be work fine)
Second problem
I have another example wich have a lot of items (for example 15 items with subitems), so when i click on the last, after that, when I try to open de drawer, this is automatically scroll to top. son I can not see the last item selectet. I need that the scroll works automatically to the selected item. Is this possible?
Example code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<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>

for your first problem just add group to your items android:checkableBehavior like first item group
<item android:title="Communicate">
<menu>
<group android:checkableBehavior="single">
<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" />
</group>
</menu>
</item>
and for second problem as for as i know it maintain selected item automatically check this after changing the android:checkableBehavior

Related

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

NavigationView menu item with heading issue

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>

Android NavigationView sub-menu header without items

I want to know if and how is possible to create a sub-menu header without items, like this:
<item android:title="Sub items"
android:layout_gravity="bottom"
>
<menu>
<item
android:title="Sub item 1" />
<item
android:title="Sub item 2" />
</menu>
</item>
If I use this it creates a sub-menu heading with two items (Sub item 1 and Sub item 2). I f I delete the subitems the header disappears to.
Here is menu_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate"
android:id="#+id/communicate">
</item>
</menu>
As you say you want to know if remove the Sub Item from the item then it is work as Normal like the upper 4 in my ScreenShot see the Output.
Output :

How can I Modify default images of Navigation Drawer.?

I want to change default Images of Navigation Drawer in Android.When I'm changing it's not working any one help me please.Thanks stack over flow.how can I do this help me please.
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="#+id/nav_manage"
android:icon="#drawable/ic_menu_manage"
android:title="Tools" />
</group>
<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>
I hope you need some pre defined vector drawables as one of your comments was pointing to it.
Try
Right Click drawable folder
New -> Vector Asset
Enable Material Icon RadioButton (enabled by default)
Click Choose button
Select one image. change Resource name if required & click Next -> Finish.
Now a new vector drawable will be created in drawable folder & you can refer it using #drawable.
Here is my .xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:title="section1"
android:checked="true"/>
<item
android:id="#+id/nav_add_event"
android:icon="#drawable/ic_menu_addevent"
android:title="section2" />
<item
android:id="#+id/nav_managenews"
android:icon="#drawable/ic_menu_manage_news"
android:title="section3" />
<item
android:id="#+id/nav_leaderboard"
android:icon="#drawable/ic_menu_leadre_board"
android:title="section3"/>
<item
android:id="#+id/nav_mypoints"
android:icon="#drawable/ic_menu_mypoint"
android:title="section4" />
<item
android:id="#+id/nav_myprofile"
android:icon="#drawable/ic_menu_my_profile"
android:title="section5" />
<item
android:id="#+id/nav_change_password"
android:icon="#drawable/ic_change_password"
android:title="section6"/>
</group>
</menu>

Android Navigation View Align Menu Items

In my app i have a nvigation view with some menu items. I want to align some of them to the center of the view. How can i achieve this correctly?
This is what i want:
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="#+id/nav_camara" android:icon="#android:drawable/ic_menu_camera"
android:title="Import" />
<item android:id="#+id/nav_gallery" android:icon="#android:drawable/ic_menu_gallery"
android:title="Gallery" />
<item android:id="#+id/nav_slideshow" android:icon="#android:drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item android:id="#+id/nav_manage" android:icon="#android:drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item android:id="#+id/nav_share" android:icon="#android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="#+id/nav_send" android:icon="#android:drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
Thanks in advance.
As far as I know this is not possible in easy way.
You could find or navigate through views hierarchy to find views or textviews you want to center.
But i think NavigationView is not designed to align things other than left or right

Categories

Resources