How to select an active menu item in NavigationView ? Menu items start Fragment , and NavigationView itself is located in activity . NavigationView initialized itself, not from the standard template from Google.

    2 answers 2

      navigation = (NavigationView) findViewById(R.id.navigation_view); navigation.getMenu().getItem(0).setChecked(false); navigation.setNavigationItemSelectedListener(new navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { int id = menuItem.getItemId(); switch (id) { case R.id.navigation_item: if (menuItem.isChecked()) { menuItem.setChecked(false); } else { menuItem.setChecked(true); } break; //и т.д. } return false; 

    menu xml:

     <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="all"> <item android:id="@+id/navigation_item" android:checked="false"/> ... </group> .... </menu> 
    • and how to be in case if instead of fragments are used Activity? (NavigationView is re-created with each launch of a different activity) - no news

    I decided to add an answer just about the Activity.

    If you use Activity instead of Fragments in NavigationView , then you can select the selected menu item using the setCheckedItem(idMenuItem); method setCheckedItem(idMenuItem); In this case, you can make a separate parent class with the initialization of NavigationView , to the signature of which from the activit class you can add the menu item id .

    Example of implementation:

    Method from parent Activity:

      protected void setupNavigationView(int idMenuItem) { initTogle(); navigationView.setCheckedItem(idMenuItem); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { drawerLayout.closeDrawers(); switch (item.getItemId()) { case R.id.nav_menu_sales: startSalesActivity(); break; case R.id.nav_menu_tenders: startTendersActivity(); break; case R.id.nav_menu_shipments: break; } return false; } }); 

    Call from the desired Activity :

     setupNavigationView(R.id.nav_menu_sales); 

    The menu.xml markup is the same as in the Fragments answer.