user5262809
user5262809

Reputation: 21

Programmatically assign color to shape

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?

Upvotes: 0

Views: 142

Answers (4)

Priya Singhal
Priya Singhal

Reputation: 1291

    @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 :)

Upvotes: 0

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

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);

Upvotes: 5

David Heisnam
David Heisnam

Reputation: 2491

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!

Upvotes: 0

Geetha
Geetha

Reputation: 190

Try the below code:

GradientDrawable bgShape = (GradientDrawable)notesLayout.getBackground();
bgShape.setColor(Color.BLACK);

Upvotes: 0

Related Questions