The Bat
The Bat

Reputation: 1115

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

Upvotes: 16

Views: 18563

Answers (12)

kirchhoff
kirchhoff

Reputation: 31

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

Objects.requireNonNull(navigationView.getCheckedItem()).setChecked(false);

Upvotes: -1

Foroogh Varmazyar
Foroogh Varmazyar

Reputation: 1565

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

menuItem.isCheckable = false

Upvotes: 3

Harsh Kothari
Harsh Kothari

Reputation: 512

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

Upvotes: 0

Rustam Nugmanov
Rustam Nugmanov

Reputation: 33

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

Upvotes: 3

Rusty Labs
Rusty Labs

Reputation: 41

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

Upvotes: 0

mhheydarchi
mhheydarchi

Reputation: 99

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

Upvotes: -1

Koorosh
Koorosh

Reputation: 527

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

Upvotes: 4

masoomyf
masoomyf

Reputation: 703

Use this:

navigationView.getCheckedItem().setChecked(false);

Upvotes: 4

David Passmore
David Passmore

Reputation: 6099

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

Upvotes: 1

Chirag Arora
Chirag Arora

Reputation: 816

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

Upvotes: 6

oiyio
oiyio

Reputation: 5925

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.

Upvotes: 27

Zakir Shikhli
Zakir Shikhli

Reputation: 424

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.

Upvotes: 12

Related Questions