Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer item remains selected Android

My navigation drawer keeps showing the last selected item.Is there any way to remove it.I want that if the user is at Home page, the navigation drawer items should be non-highlighted.

I have tried

drawer.setSelected(false);

in onResume(). But it doesn't help.

Please refer the attached screenshot, it will help understand.

See the seetings options is highlighted even when I have come back from  Settings activity

like image 627
The Bat Avatar asked Aug 30 '16 07:08

The Bat


3 Answers

In addition to the above solutions, if group element in your drawer_view.xml file includes the below attribute,

android:checkableBehavior="single"

as shown in the below example :

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single" > 
        <item
            ... />
        <item
          ... />
    </group>
</menu>

none of the above solution works. So be sure that you do not use that attribute if you do not want that highlight feature.

like image 186
oiyio Avatar answered Nov 20 '22 21:11

oiyio


I use

@Override
protected void onResume() {
    super.onResume();
    for (int i = 0; i < navigationView.getMenu().size(); i++) {
        navigationView.getMenu().getItem(i).setChecked(false);
    }
}

if did not work, also add:

itemOfMenu.setChecked(false);

to the end of onNavigationItemSelected override.

like image 22
Zakir Shikhli Avatar answered Nov 20 '22 21:11

Zakir Shikhli


Use the code below:

navigationView.getMenu().getItem(0).setChecked(false);

Call this method after you call setNavDrawer();

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));

Note: For nexus 4, support library revision 24.0.0. I recommend use navigationView.setCheckedItem(id);

like image 6
Chirag Arora Avatar answered Nov 20 '22 22:11

Chirag Arora