16

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

The Bat
  • 1,085
  • 1
  • 13
  • 31

12 Answers12

27

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.

oiyio
  • 5,219
  • 4
  • 42
  • 54
  • This did the trick. Pasted code and I didn't even see this in there. – FirstOne Aug 22 '18 at 19:23
  • android:checkable also do the job: – Grzegorz Dev Nov 07 '18 at 10:46
  • 3
    Well... Developers should first learn about these attributes before using them. It's better to wrap items like "Settings", "Rate app", "Share app" in group with `android:checkableBehavior="none"`. Other items (e.g. for selecting fragments) should be in group with `android:checkableBehavior="single"`. So use `single` for items which are supposed to be selected and `none` for items which aren't – user25 Feb 02 '19 at 21:18
12

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.

MHSaffari
  • 858
  • 1
  • 16
  • 39
Zakir Shikhli
  • 397
  • 4
  • 14
  • For Kotlin `navigationView.menu.iterator().forEach { it.isChecked = false }` – user25 Feb 02 '19 at 20:34
  • Though I don't understand why someone would even need it. If you don't want your items to be selected at all then just wrap all items in group and set `android:checkableBehavior="none"` and never call `items.checked = true` – user25 Feb 02 '19 at 21:13
6

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

Chirag Arora
  • 816
  • 8
  • 20
  • So basically you have to loop through all items and call `setChecked(false);`. It looks ugly – user25 Feb 02 '19 at 20:30
  • 4
    it's better to wrap Settings item in group with `android:checkableBehavior="none"`. Others should be in group with `android:checkableBehavior="single"` – user25 Feb 02 '19 at 21:14
  • @user25 `navigationView.setCheckedItem(id)` isn't ugly; it accepts a nav id, not an index. – Zhiyong Nov 20 '20 at 00:25
4

Use this:

navigationView.getCheckedItem().setChecked(false);
masoomyf
  • 685
  • 8
  • 15
4

set android:checkableBehavior to none in your menu resource file android:checkableBehavior="none" enter image description here

Koorosh Ghorbani
  • 507
  • 4
  • 14
3

If you're using Kotlin, this is the answer:

menuItem.isCheckable = false
Foroogh Varmazyar
  • 1,057
  • 1
  • 14
  • 18
3

The best way for me is the following code option in navigationView:

menuItem.setCheckable( false );
navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setCheckable( false );
                        mDrawerLayout.closeDrawers();
                        return true;
                        }
                    });
1

Adding to @Zakir's answer, if like me you have submenu's contained within your NavigationView, the above code does not affect any items contained in said submenus.

To solve this I implemented the below recursive method to clear all items:

private void clearCheckedItems(Menu menu){
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if(item.hasSubMenu()){
            clearMenuChecked(item.getSubMenu());
        }else{
            item.setChecked(false);
        }
    }
}
David Passmore
  • 6,089
  • 4
  • 46
  • 70
0

If you look how google apps work, you'll see the selection on touch. To do this we need to unselect last selected item (or just all of them) on drawer close event.

private int _selectedItemID = -1;

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
{
    public void onDrawerClosed(View view)
    {
        super.onDrawerClosed(view);

        NavigationView navigationView = (NavigationView)view;

        Menu menu = navigationView.getMenu();

        MenuItem menuItem = menu.findItem(_selectedItemID);

        if(menuItem != null)
        {
            menuItem.setChecked(false);
        }
    }

    public void onDrawerOpened(View drawerView)
    {
        super.onDrawerOpened(drawerView);
    }
};

public boolean onNavigationItemSelected(MenuItem item)
{
    // Handle navigation view item clicks here.
    _selectedItemID  = item.getItemId();
}
Rusty Labs
  • 41
  • 1
  • 5
0

You can use this method to remove setChecked on all items except the item user clicked

adding to @Zakir's answer I created a method which removes the previous checked item and the setChecked(true) only applies to clicked item just you have to call this method in your item onClickListener and need to pass a parameter which requires the index of the item.

 private void setItemChecked(int itemIndex) {
    for (int i = 0; i < navigationView.getMenu().size(); i++) {
        navigationView.getMenu().getItem(i).setChecked(false);
    }
    navigationView.getMenu().getItem(itemIndex).setChecked(true);
}

for example if u call this method like this setItemChecked(2) then it will setChecked(false) for all items except 2nd indexed item as which you passed as a parameter

Dharman
  • 30,962
  • 25
  • 85
  • 135
Harsh Kothari
  • 439
  • 4
  • 8
-1

On begin of The onNavigationItemSelected(MenuItem item) place this : item.setChecked(false);

mhheydarchi
  • 79
  • 1
  • 5
-1

Compilator scolds with no null check for navigationView.getCheckedItem(). This works for me:

Objects.requireNonNull(navigationView.getCheckedItem()).setChecked(false);
kirchhoff
  • 21
  • 2
  • 6
    Please, add a description to your code: what is going on here and how it solves the problem. Code-only answer is not useful. – astentx Jul 15 '21 at 08:14