androidandroid-activityandroid-menunavigation-drawer

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


Solution

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