0

I have a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/user_color">
        <shape android:shape="rectangle">
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:right="25dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/user_background" />
        </shape>
    </item>
</layer-list>

How I'm calling the shape:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/shape">

How can I programmatically change the color of the rectangle shape with id user_color?

  • 1
    possible duplicate of [Set android shape color programmatically](http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically) – Cristik Aug 25 '15 at 07:22

4 Answers4

5
int colorToPaint = getResources().getColor(android.R.color.white);// any color you want
Drawable tempDrawable = getResources().getDrawable(R.drawable.xml_layout);
LayerDrawable bubble = (LayerDrawable) tempDrawable; //cast to root element in xml
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.user_color);
solidColor.setColor(colorToPaint);
Anitha Manikandan
  • 1,160
  • 7
  • 19
0

Try the below code:

GradientDrawable bgShape = (GradientDrawable)notesLayout.getBackground();
bgShape.setColor(Color.BLACK);
Geetha
  • 190
  • 9
0

An Alternative to Anitha's answer would be-

If the LayerDrawable is already set to your ImageView use below code.

LinearLayout mLinearLayout = (LinearLayout)findViewById(R.id.my_linear_layout);
((LayerDrawable)mLinearLayout.getBackground()).findDrawableByLayerId(R.id.user_color).setColorFilter(getResources().getColor(R.color.white),PorterDuff.Mode.SRC_IN);

This way, if you already have a reference to your LinearLayout you do the whole color change in just a single line. And you don't even need to call setBackground() for the change to show!

David Heisnam
  • 2,463
  • 1
  • 21
  • 32
0
    @user5262809
         // add id to your linear layout

        <LinearLayout 
        android:id="@+id/linearLayout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/shape">

        // then in code do this :
        LinearLayout linearView = (LinearLayout) findViewById(R.id.linearLayout);

        // added Anitha's code here
    int colorToPaint = getResources().getColor(android.R.color.white);// any color you want
    Drawable tempDrawable =    getResources().getDrawable(R.drawable.xml_layout);
    LayerDrawable bubble = (LayerDrawable) tempDrawable; //cast to root element in xml
    GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.user_color);
solidColor.setColor(colorToPaint);
    // and then do this 
    linearView.setBackground(tempDrawable);



    // hope this helps you :)
Priya Singhal
  • 1,261
  • 11
  • 16