Ajitha Ajitha - 1 year ago 103
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">
<shape android:shape="rectangle">
<solid android:color="#000000" />

<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" />

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

Answer Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download