1

So inside of side_nav_bar.xml is this:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="@+id/colorNav">
<solid android:color="@color/colorPrimary"/>
</shape>

For use when using navigation drawer layout. I was wondering if there is a way to dynamically change this color inside of a fragment. For example, I have a settings fragment where the user can pick three different themes. I would like this side_nav_bar.xml shape color to match the theme. I can't figure out how to reference it programmatically in the code.

Example image below. I want that pink to change based on theme choice.

Navigation Drawer

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Jacob
  • 113
  • 1
  • 10
  • 4
    Possible duplicate of [How to change shape color dynamically?](https://stackoverflow.com/questions/7164630/how-to-change-shape-color-dynamically) – AskNilesh May 02 '18 at 04:43
  • No, I've read through that one. The thing is that guy is referencing a button. I have no idea how to reference the navigation drawer shape – Jacob May 02 '18 at 04:47
  • i see you are just applying solid color in drawable file.. in this case it's better you apply direct color to your file it would be the same result... – V-rund Puro-hit May 02 '18 at 05:08
  • How would I do that? – Jacob May 02 '18 at 05:11
  • Essentially I want to change that "android:background=" from one drawable to another – Jacob May 02 '18 at 05:11
  • @Jacob as mentioned by nilesh , when user click then , you have to get the color of the background then you can set it to your background view – Amit Ranjan May 02 '18 at 05:16
  • For instance I tried this: LinearLayout sideNavBar = findViewById(R.id.sideNavBarLayout); //The xml layout for side bar that controls background ((GradientDrawable) sideNavBar.getBackground()).setColor(Color.parseColor("#E91E63")); – Jacob May 02 '18 at 05:19
  • This throws errors 'android.graphics.drawable.Drawable android.widget.LinearLayout.getBackground()' on a null object reference – Jacob May 02 '18 at 05:20

2 Answers2

1

You can change the Navigation header color like this.

NavigationView navigationView = findViewById(R.id.nav_view);

View header = navigationView.getHeaderView(0);
     header.setBackgroundColor(getResources().getColor(R.color.my_color));
ya379
  • 81
  • 5
  • Thank you so much. That is what I was looking for. Did not realize you had to call getHeaderView(0) – Jacob May 02 '18 at 18:40
1

By using below method you can change any shape color dynamically.

public static void changeShapeColorRunTime(View view, int colorCode) {
        if (view == null) {
            return;
        }
        Drawable background = view.getBackground();
        if (background instanceof ShapeDrawable) {
            ((ShapeDrawable) background).getPaint().setColor(colorCode);
        } else if (background instanceof GradientDrawable) {
            ((GradientDrawable) background).setColor(colorCode);
        }
    }

Just Pass your view and color code to the method to change the color of your navigation bar shape color dynamically.

Riddhi Shah
  • 477
  • 7
  • 26