Ajitha
Ajitha

Reputation: 239

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?

Upvotes: 2

Views: 2624

Answers (1)

Ajitha
Ajitha

Reputation: 239

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

Upvotes: 2

Related Questions