Reputation: 925
In my app I have two items in the Navigation Drawer which represent two modes:
1) All Countries Quiz
2) Favorite Quiz
When the user selects Favorite Quiz
mode the application will check a particular condition. What I want is if the condition fails then I want to programatically set the selected property of the All Countries Quiz
list item to checked. Basically it should revert to first mode if the condition of second mode fails.
Something like this:
1) User is currently on - All Countries Quiz Mode
2) Now user clicks on - Favorite Quiz Mode. If the condition is true then the everything goes normal and selector shift to second mode:
3) But if the condition fails, then the selection should not move to 2nd mode and should remain on 1st mode:
Here's the menu list code for the items on Navigation Drawer:
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_quiz_all"
android:icon="@drawable/ic_public_black_24dp"
android:checked="true"
android:title="All Countries Quiz"/>
<item
android:id="@+id/nav_quiz_bookmarked"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Favorite Quiz"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_rate"
android:icon="@drawable/ic_star_black_24dp"
android:title="Rate this app"/>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_share_black_24dp"
android:title="Share"/>
<item
android:id="@+id/nav_feedback"
android:icon="@drawable/ic_feedback_black_24dp"
android:title="Feedback"/>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About"/>
</menu>
</item>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Settings"/>
Upvotes: 1
Views: 1207
Reputation: 23881
you can use: setCheckedItem()
method of NavigationView
snippet of code:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
if(condition){
mNavigationView.setCheckedItem(R.id.nav_item); //yOur Item
}
mNavigationView.setCheckedItem(item.getItemId());
}
EDIT
you can use the following code snippet: call it in your activity when the condition applies..
mNavigationView.post(new Runnable() {
@Override
public void run() {
navigationView.setCheckedItem(R.id.nav_item);
}
});
Upvotes: 2