Reputation: 802
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>
Upvotes: 5
Views: 3078
Reputation: 365
try this line of code with itemChecked
is the index of the menu selected
navigationView.getMenu().getItem(itemChecked).setChecked(true);
Upvotes: 1
Reputation: 4169
Following code open first fragment of NavigationView and check it on NavigationView. For meaning of savedInstancesState == null condition: https://stackoverflow.com/a/26855936/4606368
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.
Upvotes: 0
Reputation: 29
use setChecked
method of item instead of navigationView
navigationView.post(new Runnable() {
@Override
public void run() {
navigationView.getMenu().getItem(0).setChecked(true);
}
});
Upvotes: -1
Reputation: 6363
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>
Upvotes: 2
Reputation: 802
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);
}
});
Upvotes: 8
Reputation: 803
Try this way :
menuItem.setCheckable(true);
menuItem.setChecked(true);
if (mPreviousMenuItem != null && mPreviousMenuItem != menuItem) {
mPreviousMenuItem.setChecked(false);
}
mPreviousMenuItem = menuItem;
Upvotes: 0