简体   繁体   中英

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

For navigation view you have to write a color selector like: nav_view_item_background

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/primary" android:state_checked="true" />
        <item android:drawable="@android:color/transparent" />
</selector>

and set app:itemBackground="@drawable/nav_view_item_background"

For text color you have to same thing. Create a textColor selector and set app:itemTextColor

For Drawer Layout return true (In Navigation item selection) change the color and selection.

<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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM