chobo2 chobo2 - 5 months ago 39
Android Question

How to change shape color dynamically?

I have

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFFF00" />
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
</shape>

<TextView
android:background="@drawable/test"
android:layout_height="45dp"
android:layout_width="100dp"
android:text="Moderate"
/>


So now I want this shape to change colors based on information I get back from a web service call. So it could be maybe yellow or green or red or whatever depending on the color I receive from the web serivce call.

How can I change the color of the shape? Based on this information?

Answer

You could modify it simply like this

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
Comments