0

I'm developing a social network app, where I have implemented the navigation drawer using DrawerLayout with NavigationView. I've customized the background color for each item in the menu using state-list drawable. The problem is that when I select different items in the drawer, the pressed background color for the item changes to blue sometimes, instead of the specified one in the drawable. This behavior goes away if I rotate a device 2-4 times and select different items, but some times passes by and that background color shows up again. This looks really odd to me and I don't know what to do about it. So my question is: how to prevent that blue color from showing up?

Correct pressed background color for photos item: correct

Incorrect pressed background color for photos item: incorrect

My NavigationView:

<android.support.design.widget.NavigationView
     android:id="@+id/drawer_navigation_view"
     android:layout_width="300dp"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     app:headerLayout="@layout/drawer_header"
     app:menu="@menu/drawer_menu"
     app:itemIconTint="@color/drawer_menu_item_icon_color"
     app:itemTextColor="@color/drawer_menu_item_text_color"
     app:itemBackground="@drawable/drawer_menu_item_background"/>

drawer_menu_item_background.xml:

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item
         android:state_pressed="true"
         android:drawable="@color/drawer_menu_item_bg_pressed"/>  <!-- #F0F0F0 -->
     <item
         android:state_checked="true"
         android:drawable="@color/drawer_menu_item_bg_checked"/>  <!-- #efefef -->
     <item
         android:drawable="@android:color/transparent"/>
  </selector>

Edit:

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="colorPrimary">#44AD79</color>

   <color name="register_login_edit_text_color">#222222</color>
   <color name="register_login_edit_text_color_hint">#999999</color>
   <color name="register_login_button_bg">#eceef1</color>
   <color name="register_login_button_bg_pressed">#d2d4d6</color>
   <color name="register_login_button_text">#8cdbca</color>
   <color name="register_login_button_text_enabled">#26ae90</color>

   <color name="darker_bg">#EBEDF0</color>

   <color name="drawer_header_bg">#88C973</color>
   <color name="drawer_menu_text_color">#303030</color>
   <color name="drawer_menu_item_bg_checked">#efefef</color>
   <color name="drawer_menu_item_bg_pressed">#F0F0F0</color>

   <color name="white">#ffffff</color>
   <color name="black">#000000</color>
   <color name="light_red">#ff6861</color>
   <color name="light_gray">#aaaaaa</color>
   <color name="dark_gray">#767676</color>
   <color name="greenish">#67b58e</color>

   <color name="settings_item_color">#222222</color>
   <color name="settings_title_divisor">#d2d4d6</color>
   <color name="settings_item_divisor">#EBEDF0</color>
   <color name="settings_item_bg_pressed">#eaeef1</color>
   <color name="change_pass_edit_text_color_hint">#CDD2D4</color>
   <color name="change_pass_edit_text_bottom_color_line">#d2d4d6</color>
   <color name="change_pass_button_disabled_bg">#d2d4d6</color>
   <color name="change_pass_button_pressed_bg">#72CFA1</color>

   <color name="friends_tab_text_color">#D7DEE0</color>

   <color name="profile_error_message_button_bg">#CDE6C5</color>
   <color name="profile_error_message_button_bg_pressed">#ADDB9E</color>
   <color name="profile_error_message_button_text_color">#489E2B</color>
   <color name="profile_main_buttons_bg_pressed">#e8e8e8</color>
   <color name="profile_wall_divisor">#D7D9DB</color>
</resources>

drawer_menu_item_icon_color.xml:

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

drawer_menu_item_text_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:color="@color/colorPrimary" android:state_checked="true"/>
   <item android:color="@color/drawer_menu_text_color"/>
</selector>
mars885
  • 73
  • 3
  • 9
  • Please post the code you use to change your color! :) – Joel Min May 31 '16 at 02:15
  • @JoelMin I posted the drawable that sets the background color for each item in the navigation drawer. Apart from that, I don't have any other code that changes the color. – mars885 May 31 '16 at 10:56
  • Post your color.xml file. It would be useful to determine the problem – iSrinivasan27 Jun 07 '16 at 04:53
  • I checked your color.xml and it doesn't have the blue color what appeared in incorrect image. So it may occur due to predefined color – iSrinivasan27 Jun 08 '16 at 12:05
  • @i-Droid Agree. I had a similar issue with the up button on the toolbar. I managed to remove it by overriding `selectableItemBackground` attribute of the toolbar style and supplying my custom drawable. Unfortunately, I do not know how to do that with a navigation drawer. – mars885 Jun 08 '16 at 12:35
  • What is up button in toolbar?? – iSrinivasan27 Jun 08 '16 at 12:55
  • @i-Droid This: [image](https://saptarga.files.wordpress.com/2015/11/up-button1.png?w=350&h=200&crop=1) – mars885 Jun 08 '16 at 13:00

0 Answers0