Search code examples
androidxmldrawerlayoutnavigationview

NavigationView selected item color


i have two layouts that use a drawerLayout and they use the same code for the navigationView, the problem is that one of them changes the color of the selected item while the other doesn't even though it's the same exact code. here is the xml code:

 <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view_passager"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/menu_passager"  />

and java for the 1st layout:

    @Override
public boolean onNavigationItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.acceuil_passager_item:
            toolbar.setTitle("Accueil");
            fm.beginTransaction().replace(R.id.frame_passager, new AcceuilPassagerFragment()).commit();
            break;

        case R.id.profile_item:
            toolbar.setTitle("Profil");
            fm.beginTransaction().replace(R.id.frame_passager, new PassagerProfileFragment()).commit();
            break;

        case R.id.historique_voyages_item_pass:
            toolbar.setTitle("Historique des voyages");
            fm.beginTransaction().replace(R.id.frame, new ListeTrajetsFragment()).commit();
            break;
        case R.id.futurs_voyages_item_pass:
            toolbar.setTitle("Futurs voyages");
            fm.beginTransaction().replace(R.id.frame_passager, new FutursVoyagesFragment()).commit();
            break;
        case R.id.log_out_item_pass:
            Intent intent = new Intent(PassagerActivity.this, LoginActivity.class);
            startActivity(intent);
            mAuth.signOut();
            finish();
            Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
            break;

        default:
            break;
    }
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

and the java code for the 2nd layout :

    @Override
public boolean onNavigationItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.acceuil_item_conducteur:
            setUpToolbar(item);
            fm.beginTransaction().replace(R.id.frame_conducteur, new AcceuilConducteurFragment()).commit();
            break;

        case R.id.profile_item_cond:
            setUpToolbar(item);
            fm.beginTransaction().replace(R.id.frame_conducteur, new ConducteurProfileFragment()).commit();
            break;

        case R.id.historique_voyages_item_cond:
            setUpToolbar(item);
            fm.beginTransaction().replace(R.id.frame_conducteur, new HistoriqueVoyagesFragment()).commit();
            break;

        case R.id.log_out_item_cond:
            Intent intent = new Intent(ConducteurActivity.this, LoginActivity.class);
            startActivity(intent);
            mAuth.signOut();
            finish();
            Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
            break;
        default:
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

and here is the first layout

and the 2nd one

ps: "Profil" is selected in both layouts


Solution

  • <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <group
        android:id="@+id/menu_grp"
        android:checkableBehavior="single">
    

    Turned out checkableBehavior = "single" is what made the menu selected item checked and what was missing in my other layout