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
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.
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"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.