44

I wanted to set the background color of my Navigation drawer in java but it seems as setBackgroundColor and all similar methods have no effect. Only the XML line android:background="@color/mycolor" is working. If remove the xml line and try one of the methods the drawer just stays transparent.

Any ideas?

mike.b93
  • 1,989
  • 2
  • 18
  • 31

3 Answers3

92

In your activity_main.xml include the following

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/menu_drawer"
    android:background="@color/color_navigation_list_background"
    app:itemIconTint="@color/color_selector_navigation_item"
    app:itemTextColor="@color/color_selector_navigation_item"/>
Goodlife
  • 3,822
  • 2
  • 24
  • 23
28

I just solved my own problem.

I totally forgot that it's not the DrawerLayout I want to set the background to, but the ListView inside.

I admit I made it somewhat hard for you guys without adding code to my post -.-'

So instead of:

private DrawerLayout mDrawerLayout;
(...)
mDrawerLayout.setBackgroundResource(int);

I had to do this:

private ListView mDrawerList;
(...)
mDrawerList.setBackgroundResource(int);
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
mike.b93
  • 1,989
  • 2
  • 18
  • 31
4

cast your navigation drawer to navigationView and do as follows

 navigationView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
Al Walid Ashik
  • 1,545
  • 20
  • 32