2

I am trying to change color of a shape in programmatically, but its not working.

Here is the shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2dp" android:color="#fff" />
</shape>

here is how i am using it as background of a button

<Button                
  android:id="@+id/ibtn_EA_ColorPick_new"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:background="@drawable/round_button"
  />

and here is how i am changing its color

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
bgShape.setColor(Color.RED);

But when ever I change the background color, it removes button from screen.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
AddyProg
  • 2,960
  • 13
  • 59
  • 110

1 Answers1

10

Change the code as below

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
 bgShape.mutate()
 bgShape.setColor(Color.RED);
Manish
  • 1,215
  • 10
  • 29