NavigationView with DrawerLayout setCheckedItem not working
Asked Answered
C

6

5

I want let NavigationView check one item when app startup. But i found NavigationView.setCheckedItem(R.id.xxx) not working. And i also tried navigationView.getMenu().findItem(R.id.xxx).setChecked(true), same result.

I've already set checkableBehavior to single.

<item android:title="@string/sliding_menu_group_places">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/xxx" />

                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/xxx" />

                ...
            </group>
        </menu>
    </item>

But there is one way worked:

drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            navigationView.setChecked(R.id.xxx);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerStateChanged(int newState) {
        }
    });

The navigation item would be checked in onDrawerOpened callback. I've searched a lot in stackoverflow but none methods work for me. Who can help me about this.

EDIT-1

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/xxx"
        android:icon="@drawable/xxx"
        android:title="@string/recent_display_name" />

    <item android:title="@string/sliding_menu_group_places">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/my_files_display_name" />

                <item
                    android:id="@+id/nav_item_sdcard"
                    android:icon="@drawable/ic_nav_sdcard"
                    android:title="@string/storage_display_name" />
            </group>
        </menu>
    </item>

    <item android:title="@string/sliding_menu_group_tool">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/clean_display_name" />
            </group>
        </menu>
    </item>

    <item android:title="@string/sliding_menu_group_settings">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/settings_display_name" />

                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/exit_display_name" />
            </group>

        </menu>
    </item>
</group>

Cryptoclastic answered 12/10, 2016 at 5:29 Comment(11)
try navigationView.getMenu().getItem(pos).setChecked(true);Cyzicus
just set , android:checked="true" inside your <item android:id="@+id/xxx" android:icon="@drawable/xxx" android:title="@string/xxx" /> . no need to set it manually in your java file.Galagalactagogue
mDrawerList.setItemChecked(position, true); here mDrawerList is you ListView, try thisEpps
@Cyzicus still not workingCryptoclastic
is ur drawer listener works fine? try calling navigationView.getMenu().getItem(pos).setChecked(true); from anywere elseCyzicus
@Cyzicus call navigationView.getMenu().getItem(pos).setChecked(true) in drawer listener works fine, but not working in other places.Cryptoclastic
@Cyzicus You can see my edit-1 for menu's xml.Cryptoclastic
may be ur navigation view not ready while u are calling the setCheckedCyzicus
@Cyzicus Yeah, I test it according to ur guess. Call navigationView.setCheckedItem(R.id.xxx); after delay 2s, it's working. But i can't figure out why and how to solve it.Cryptoclastic
u may try calling it in onCreateOptionMenu() or OnPrepareOptionMenu(),but i am not sure it will work,i didn't tried this beforeCyzicus
@Cyzicus You can see my answer for detail. I solved by call navigationView.post(). Thank you very much :-)Cryptoclastic
C
8

Thanks to @darwin.

Reason: navigationView.setCheckedItem(R.id.xxx) not working because the NavigationView isn't ready for it.

Solution:

navigationView.post(new Runnable() {
        @Override
        public void run() {
            navigationView.setCheckedItem(id);
        }
    });
Cryptoclastic answered 12/10, 2016 at 7:4 Comment(1)
It worked! How is it possible that NavigationView is not ready?Alliterative
G
2

Menu items must be checkable.

<item android:title="@string/sliding_menu_group_places">
    <menu>
        <group> <!-- removed behavior -->
            <item
                android:id="@+id/xxx"
                android:icon="@drawable/xxx"
                android:title="@string/xxx"
                android:checkable="true" /> <!-- added 'checkable' -->

            <item
                android:id="@+id/xxx"
                android:icon="@drawable/xxx"
                android:title="@string/xxx"
                android:checkable="true" /> <!-- added 'checkable' -->

            ...
        </group>
    </menu>
</item>
Gamp answered 19/1, 2017 at 11:20 Comment(0)
A
1

try this line of code with itemChecked is the index of the menu selected

 navigationView.getMenu().getItem(itemChecked).setChecked(true);
Arched answered 3/7, 2019 at 14:8 Comment(0)
E
0

Try this way :

 menuItem.setCheckable(true);
                menuItem.setChecked(true);
                if (mPreviousMenuItem != null && mPreviousMenuItem != menuItem) {
                    mPreviousMenuItem.setChecked(false);
                }
                mPreviousMenuItem = menuItem;
Evaevacuant answered 12/10, 2016 at 6:44 Comment(1)
Thanks anyway bro, but this not the real solution.Cryptoclastic
R
0

Following code open first fragment of NavigationView and check it on NavigationView. For meaning of savedInstancesState == null condition: https://mcmap.net/q/529932/-why-do-you-check-for-savedinstancestate-null-when-adding-fragment

    if (savedInstanceState == null) {
                Fragment fragment = new EventsUpcoming();
                getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragment, "Upcoming").commit();
                navigationView.setCheckedItem(R.id.nav_eventupcoming);
                toolbar.setTitle(R.string.events_upcoming);
}

Add this code in OnCreate method of your activity.

Registrant answered 13/2, 2018 at 23:10 Comment(0)
T
-1

use setChecked method of item instead of navigationView

navigationView.post(new Runnable() {
   @Override
   public void run() {
      navigationView.getMenu().getItem(0).setChecked(true);
   }
});
Troposphere answered 1/2, 2018 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.