Navigation Drawer item remains selected Android
Asked Answered
J

12

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

Juvenescence answered 30/8, 2016 at 7:26 Comment(2)
If you click on it drawer is getting closed or not? can you share your xml and code tooMcwhirter
Is there any actions you've set for these items on click?Loftis
R
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);

Regine answered 30/8, 2016 at 7:32 Comment(3)
So basically you have to loop through all items and call setChecked(false);. It looks uglyHumbug
it's better to wrap Settings item in group with android:checkableBehavior="none". Others should be in group with android:checkableBehavior="single"Humbug
@Humbug navigationView.setCheckedItem(id) isn't ugly; it accepts a nav id, not an index.Consume
S
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.

Sandlin answered 26/12, 2017 at 13:22 Comment(3)
This did the trick. Pasted code and I didn't even see this in there.Alannaalano
android:checkable also do the job: <item android:title="my clickable but non-selectable option" android:checkable="false" ...>Custumal
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'tHumbug
P
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.

Pesade answered 23/7, 2017 at 8:44 Comment(2)
For Kotlin navigationView.menu.iterator().forEach { it.isChecked = false }Humbug
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 = trueHumbug
R
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);

Regine answered 30/8, 2016 at 7:32 Comment(3)
So basically you have to loop through all items and call setChecked(false);. It looks uglyHumbug
it's better to wrap Settings item in group with android:checkableBehavior="none". Others should be in group with android:checkableBehavior="single"Humbug
@Humbug navigationView.setCheckedItem(id) isn't ugly; it accepts a nav id, not an index.Consume
H
4

Use this:

navigationView.getCheckedItem().setChecked(false);
Harmonia answered 12/1, 2019 at 12:17 Comment(1)
Much more efficient instead of iterating over each menu itemProtamine
K
4

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

Keitloa answered 7/2, 2019 at 12:16 Comment(0)
E
3

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

menuItem.isCheckable = false
Ephemeron answered 15/12, 2019 at 19:30 Comment(0)
M
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;
                        }
                    });
Millstone answered 27/5, 2020 at 1:6 Comment(1)
best answer, exactly does the same what i was looking for.Culinary
A
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);
        }
    }
}
Antarctica answered 21/10, 2018 at 16:15 Comment(0)
H
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();
}
Hannover answered 10/5, 2019 at 3:15 Comment(0)
L
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

Laminated answered 20/9, 2020 at 4:27 Comment(0)
X
-1

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

Xylem answered 30/3, 2019 at 20:23 Comment(3)
The question is about "selected", not about "checked".Indeciduous
Please Check android documents,MenuItems will deselect with setChecked(false)Xylem
please improve your answer with this explanation. This is better than just commentingIndeciduous
T
-1

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

Objects.requireNonNull(navigationView.getCheckedItem()).setChecked(false);
Touch answered 14/7, 2021 at 15:7 Comment(1)
Please, add a description to your code: what is going on here and how it solves the problem. Code-only answer is not useful.Glynda

© 2022 - 2024 — McMap. All rights reserved.