Change xml drawable shape color as src programmatically

505 views Asked by At

Having trouble with changing the color of a shape in a drawable XML.

I have this xml drawable file (which creates 3 circles one inside the other):

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval"
        android:id="@+id/card_layer1">
        <solid android:color="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>
<item android:top="2dp" android:left="2dp" android:right="2dp" android:bottom="2dp">
    <shape android:shape="oval"
        android:id="@+id/card_layer2">
        <solid android:color="#ff0000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>

<item android:top="4dp" android:left="4dp" android:right="4dp" android:bottom="4dp">
    <shape android:shape="oval"
        android:id="@+id/card_layer3">
        <solid android:color="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <size
            android:width="5dp"
            android:height="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </shape>
</item>

I want to be able to change programmatically the color of each layer of these shapes.

I found this answer: Change shape's color, but it doesn't work well for me because it changes the background attribute, and I am using the src attribute so I think I need a different solution.

Basically, I need to find a way to do something that will get me the src of a view and not the background:

View v = findViewById(R.id.layout_id);

LayerDrawable bgDrawable = (LayerDrawable)v.getBackground(); // get src and not background

But, I couldn't find anything that would do that..

Any help will be appreciated!

0

There are 0 answers