Ibtihaj Uddin
Ibtihaj Uddin

Reputation: 138

navigation drawer item not checked by default

Here's my xml for the navigation drawer items

<item android:title="Movies"
    android:checkable="true">
    <menu>
        <group
            android:id="@+id/movies" 
            android:checkableBehavior="single">
            <item // this needs to be checked by default
                android:id="@+id/upComingMovies"
                android:title="Up Coming"
                android:icon="@drawable/ic_movies"/>

            <item
                android:id="@+id/inCinemaMovies"
                android:title="In Cinemas"
                android:icon="@drawable/ic_movies"/>

            <item
                android:id="@+id/popularMovies"
                android:title="Popular Now"
                android:icon="@drawable/ic_movies"/>

            <item
                android:id="@+id/topRatedMovies"
                android:title="Top Rated"
                android:icon="@drawable/ic_movies"/>
        </group>
    </menu>
</item>

My activity code

navigationView = findViewById(R.id.nav_view);
    navigationView.getMenu().getItem(0).setChecked(true);

But this doesn't keep the item checked by default, but when i click on any item it gets checked

Upvotes: 0

Views: 776

Answers (2)

Krasavello13
Krasavello13

Reputation: 333

Set in you xml android:checked="true" for item what you want set as default.

<item 
    android:id="@+id/upComingMovies"
    android:title="Up Coming"
    android:checked="true"
    android:icon="@drawable/ic_movies"/>

Upvotes: 1

Viraj Patel
Viraj Patel

Reputation: 2393

The issue is, you are setting property setChecked(true) after findviewbyId.
First set you navigation menu then set the property.
And use navigationView.setCheckedItem(id); which is introduce in API 23.

Check this link for more info

Upvotes: 3

Related Questions