I have a certain my_drawable.xml , it looks like this:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- view background color --> <solid android:color="@color/color_chat_task_title" > </solid> <corners android:bottomLeftRadius="20dp" android:bottomRightRadius="20dp"> </corners> </shape> 

Question: how can I change the background color in java code?

  • 2
    look here - Maxim Kuznetsov

2 answers 2

Try this:

.xml

 ... <ImageView android:id="@+id/imgShape" android:background="@drawable/my_drawable"/> ... 

.java

 ImageView imgShape = (ImageView) findViewById(R.id.imgShape); GradientDrawable drawable = (GradientDrawable) imgShape.getBackground(); drawable.setColor(Color.BLUE); 

    You can make a second xml file with a different color and set the method setBackgroundResource .