How to change shape color dynamically?
Solution 1
You could modify it simply like this
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
Solution 2
For me, it crashed because getBackground
returned a GradientDrawable
instead of a ShapeDrawable
.
So i modified it like this:
((GradientDrawable)someView.getBackground()).setColor(someColor);
Solution 3
This works for me, with an initial xml resource:
example.setBackgroundResource(R.drawable.myshape);
GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000"));
gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);
Result of the above: http://i.stack.imgur.com/hKUR7.png
Solution 4
You can build your own shapes in Java. I did this for an iPhone like Page Controler and paint the shapes in Java:
/**
* Builds the active and inactive shapes / drawables for the page control
*/
private void makeShapes() {
activeDrawable = new ShapeDrawable();
inactiveDrawable = new ShapeDrawable();
activeDrawable.setBounds(0, 0, (int) mIndicatorSize,
(int) mIndicatorSize);
inactiveDrawable.setBounds(0, 0, (int) mIndicatorSize,
(int) mIndicatorSize);
int i[] = new int[2];
i[0] = android.R.attr.textColorSecondary;
i[1] = android.R.attr.textColorSecondaryInverse;
TypedArray a = this.getTheme().obtainStyledAttributes(i);
Shape s1 = new OvalShape();
s1.resize(mIndicatorSize, mIndicatorSize);
Shape s2 = new OvalShape();
s2.resize(mIndicatorSize, mIndicatorSize);
((ShapeDrawable) activeDrawable).getPaint().setColor(
a.getColor(0, Color.DKGRAY));
((ShapeDrawable) inactiveDrawable).getPaint().setColor(
a.getColor(1, Color.LTGRAY));
((ShapeDrawable) activeDrawable).setShape(s1);
((ShapeDrawable) inactiveDrawable).setShape(s2);
}
hope this helps. Greez Fabian
Solution 5
Maybe someone else need to change color in the XML without create multiple drawables like I needed. Then make a circle drawable without color and then specify backgroundTint for the ImageView.
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
</shape>
And in your layout:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/circle"
android:backgroundTint="@color/red"/>
Edit:
There is a bug regarding this method that prevents it from working on Android Lollipop 5.0 (API level 21). But have been fixed in newer versions.
chobo2
Updated on July 12, 2022Comments
-
chobo2 almost 2 years
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?
-
chobo2 over 12 yearsWhat is btn.getBackground?
-
Ron over 12 yearsbtn can be any view for which you set the shape drawable from xml. So you can get the drawable back using getBackground() on that view and change its color.
-
chobo2 over 12 yearsHmm having a hard time to convert this code to monodroid ShapeDrawable bgShape = (ShapeDrawable) Resources.GetDrawable(Resource.Drawable.test); just crashes
-
prolink007 over 10 yearsI am getting
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable
when trying this suggestion. -
Singh Arjun about 10 yearsgetting cast exception
-
ndgreen almost 10 yearsDoes not work. You will get a cast error. Needs a fix or another answer accepted
-
W3hri almost 9 yearsThis works just fine. The previous comments are outdates since the answer got edited!
-
Ruben2112 over 7 yearsWorks great for me as well.
-
naveed148 over 7 yearsshoul use GradientDrawable bgShape = (GradientDrawable)btn.getBackground().getCurrent();
-
Beshoy Fayez over 7 yearsWorks fine but take care that it will change the shape color so next time you use it, it will have the latest color you set. So you need to set the color every time you use this shape drawable.
-
codemax about 7 yearsWorks for me after several trial and error before finding this answer! Thank you!
-
Sanal Varghese over 6 yearsThis is the correct answer. Both the upwoted above answers were not worked for me. I am getting exception on both of it.
-
André Luiz Reis about 6 yearsIt works only if your drawable XML has only the shape tag inside otherwise it will have a cast error.
-
Rakesh Yadav about 6 yearsNot a perfect one, but helped me to get the solution.
-
Prakash Palnati almost 6 yearsWorks fine for me.
-
Alin over 5 yearsI got a java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.GradientDrawable but (GradientDrawable)btn.getBackground().getCurrent(); ended up working
-
Sagar almost 5 yearsyou can try for
GradientDrawable bgShape = (GradientDrawable) bubble_text.getBackground().mutate(); bgShape.setColor(color); bgShape.setStroke(1, color);
-
Abhi S about 2 yearsHow to get that change color drawable shape as a Drawable?