Change the color of a selected item in a Navigation Drawer

0

I have a problem with a navigation drawer, when I select an item that I have inside the drawer if it changes color and it is selected with a gray shadow, the problem is when I start creating groups in my item for the drawer of navigation, if the item works, send me to another fragment, but they do not change color anymore and they are not selected.

In the image if I press which item I want is not highlighted in another group, but in the group called Settings, if I select the tools option that is within that group, it is not highlighted or shadows.

This is my file that contains all my items:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/inicio"
            android:icon="@drawable/blog"
            android:title="Inicio"
            />

        <item
            android:id="@+id/convenios"
            android:icon="@drawable/convenios"
            android:title="Convenios"
            />

        <item
            android:id="@+id/documentos"
            android:icon="@drawable/archivos"
            android:title="Documentos"
            />

        <item
            android:id="@+id/guia"
            android:icon="@drawable/guia"
            android:title="Guia"
            />

        <item
            android:id="@+id/contactos"
            android:icon="@drawable/contacto"
            android:title="Contactanos"
            />

        <item
            android:id="@+id/ubicacion"
            android:icon="@drawable/posicion"
            android:title="Ubicacion"
            />

        <item
            android:id="@+id/cerrar"
            android:icon="@drawable/cerrars"
            android:title="Cerrar sesión"
            />

    </group>

    <item android:title="Settings">
        <menu>
            <item
                android:id="@+id/nav_manage"
                android:icon="@drawable/contacto"
                android:title="Tools" />
        </menu>
    </item>
</menu>

I create a resource file in a folder called color, that is the file that I create:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#8E0000" android:state_checked="true" />
    <item android:color="#000000" />
</selector>

That file that I created for the color change of the selected items I use in the xml file where I create my navigation drawer, in these two lines is where I use my file to highlight the selection of the items:

app: itemIconTint="@ color / drawer_items" app: itemTextColor="@ color / drawer_items"

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".Principal">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@color/drawer_items"
        app:itemTextColor="@color/drawer_items"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

and finally I have the class where I implement the navigation drawer;

public class Principal extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        //Toolbar de la aplicacion en la vista principal
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("Sección 15");
        toolbar.setSubtitle("Inicio");

        Inicio inicio = new Inicio();
        android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.qwe, inicio).commit();
        //Cajon de navegacion para la vista principal
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        onNavigationItemSelected(navigationView.getMenu().getItem(0));

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();

        if(id == R.id.inicio){
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Inicio");
            Inicio inicio = new Inicio();
            android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.qwe, inicio).commit();

        }else if(id == R.id.convenios){
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Convenios");
            Convenios convenios = new Convenios();
            android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.qwe, convenios).commit();

        }else if (id == R.id.ubicacion){
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Ubicación");
            //Ubicacion ubicacion = new Ubicacion();
            //android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            //manager.beginTransaction().replace(R.id.qwe, ubicacion).commit();

        }else if (id == R.id.documentos){
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Documentos");
            //Documentos documentos = new Documentos();
            //android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            //manager.beginTransaction().replace(R.id.qwe, documentos).commit();

        }else if (id == R.id.guia){
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Guia");
            //Guia guia = new Guia();
            //android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            //manager.beginTransaction().replace(R.id.qwe, guia).commit();

        }else{
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setSubtitle("Contacto");
            //Contacto contacto = new Contacto();
            //android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
            //manager.beginTransaction().replace(R.id.qwe, contacto).commit();

        }
        //Este codigo nos permite mostrar que menu esta seleccionado
        //para poder identificar que menu esta en uso
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override public void onBackPressed() {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}
    
asked by Enrique Espinosa 02.10.2018 в 18:18
source

0 answers