Ajitha Ajitha - 22 days ago 5
Android Question

Change the shape color in an Android XML

I have android drawable that I'm going to apply to the background of several TextViews

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>

<item android:id="@+id/clr"
android:left="0.5dp" android:right="0.5dp" android:top="0.5dp" android:bottom="0.5dp" >
<shape android:shape="rectangle">
<solid android:color="#FFF000" />
</shape>
</item>
</layer-list>


I want to change the color of this programmatically. How can I do that?

Answer

It's possible to do it like this

// "cellLabel" background colour of textview or button etc. 
LayerDrawable layers = (LayerDrawable) cellLabel.getBackground();
// drawable item id
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.clr));
shape.setColor(my_color);