title: Setting background color for programmatically added menu item of Navigation View when checked in Android
body: <p>I am developing an Android app. In my app, I am adding menu item to navigation view programmatically. But I am having a problem with designing that items when checked.My problem is now only text color is changed when item is checked. But what want is I want item highlighted with background like below.</p>
  
  <p><a href="https://i.stack.imgur.com/nKDk4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nKDk4.png" alt="enter image description here"></a></p>
  
  <p>I am adding menu item programmatically like this</p>
  
  <pre><code>menu.add(CATEGORY_MENU_GROUP_ID, itemId, i + 1, title).setIcon(dIcon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                      @Override
                      public boolean onMenuItemClick(MenuItem item) {
  
                          item.setChecked(true);
                          return true;
                      }
                  });
  </code></pre>
  
  <p>I followed this question <a href="https://stackoverflow.com/questions/31221637/android-navigation-view-item-menu-background-color">Android - Navigation View item menu background color</a></p>
  
  <p>So I created background item like this (nav_item_bg.xml)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" &gt;
  
      &lt;!-- view background color --&gt;
      &lt;solid android:color="@color/lightGray" &gt;
      &lt;/solid&gt;
  
      &lt;!-- view border color and width --&gt;
      &lt;stroke
          android:width="1dp"
          android:color="@color/lightGray" &gt;
      &lt;/stroke&gt;
  
      &lt;!-- Here is the corner radius --&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>My navigation view xml</p>
  
  <pre><code>&lt;android.support.design.widget.NavigationView
          app:itemIconTint="@drawable/drawer_item"
          app:itemTextColor="@drawable/drawer_item"
          android:id="@+id/left_nv_view"
  
          app:headerLayout="@layout/header_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start" /&gt;
  </code></pre>
  
  <p>This is my drawer_item.xml in drawable</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:color="@color/colorAccent"  android:state_enabled="true" android:state_checked="true" /&gt;
      &lt;!--state is enabled and not checked--&gt;
      &lt;item android:color="@color/black" android:state_enabled="true" android:state_checked="false" /&gt;
      &lt;!--state (menu item) is disabled --&gt;
      &lt;item android:state_enabled="false" android:color="@color/lightGray"  /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>When I run, background of all menu item in navigation view are changed even they are not checked. What I want is just changed when selected. How can I achieve it?</p>
  
  <p>I tried changing the nav_item_bg.xml to this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:color="@color/lightGray"  android:state_enabled="true" android:state_checked="true" /&gt;
      &lt;!--state is enabled and not checked--&gt;
      &lt;item android:color="@color/white" android:state_enabled="true" android:state_checked="false" /&gt;
      &lt;!--state (menu item) is disabled --&gt;
      &lt;item android:state_enabled="false" android:color="@color/white"  /&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>But it is not working.</p>