-3

I'm trying to change the background color of my Navigation drawer menu. When an item is pressed it changes to red and what I wanted to achieve is it stays red after clicking because it is the current active item in the Navigation and even the drawer is closed.

I created a drawable xml where I set the background of the item (item_bg.xml) here's my code

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/brickred" />
<item android:state_activated="true" android:drawable="@color/brickred" />
<item android:state_active="true" android:drawable="@color/brickred" />
<item android:state_pressed="true" android:drawable="@color/brickred" />
<item android:state_focused="true" android:drawable="@color/brickred" />
</selector>

and calls it to my navigation_activity.xml like this

<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"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_navigation_drawer"
    android:background="@color/tableHeader"
    app:itemBackground="@drawable/item_bg"/>

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

Any suggestions on how to achieve this? thank you.


Here's my NavigationActivity looks like:

 @SuppressWarnings("StatementWithEmptyBody")
  @Override
   public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    Intent goToSreen;

    if (id == R.id.nav_dashboard) {
        goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class);
        startActivity(goToSreen);
        navigationView.setCheckedItem(R.id.nav_dashboard);
    } 

but it still goes back to its original color after the drawer is closed.

natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • Try: In your style: `` and include the style in `android.support.design.widget.NavigationView ` !!! ` app:theme="@style/YourThemeNameFor.NavigationDrawer" ` – Satan Pandeya May 30 '17 at 04:15
  • Similar question has been asked [here](https://stackoverflow.com/questions/33956978/navigationviewchange-color-of-selected-item) – Kunal Chawla May 30 '17 at 04:19

1 Answers1

3

I tried this

navigationView.getMenu().getItem(1).setChecked(true);

Check here

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38