How to change shape color dynamically?

133,382

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.

Share:
133,382
chobo2
Author by

chobo2

Updated on July 12, 2022

Comments

  • chobo2
    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
    chobo2 over 12 years
    What is btn.getBackground?
  • Ron
    Ron over 12 years
    btn 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
    chobo2 over 12 years
    Hmm having a hard time to convert this code to monodroid ShapeDrawable bgShape = (ShapeDrawable) Resources.GetDrawable(Resource.Drawable.test); just crashes
  • prolink007
    prolink007 over 10 years
    I am getting java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable when trying this suggestion.
  • Singh Arjun
    Singh Arjun about 10 years
    getting cast exception
  • ndgreen
    ndgreen almost 10 years
    Does not work. You will get a cast error. Needs a fix or another answer accepted
  • W3hri
    W3hri almost 9 years
    This works just fine. The previous comments are outdates since the answer got edited!
  • Ruben2112
    Ruben2112 over 7 years
    Works great for me as well.
  • naveed148
    naveed148 over 7 years
    shoul use GradientDrawable bgShape = (GradientDrawable)btn.getBackground().getCurrent();
  • Beshoy Fayez
    Beshoy Fayez over 7 years
    Works 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
    codemax about 7 years
    Works for me after several trial and error before finding this answer! Thank you!
  • Sanal Varghese
    Sanal Varghese over 6 years
    This 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
    André Luiz Reis about 6 years
    It works only if your drawable XML has only the shape tag inside otherwise it will have a cast error.
  • Rakesh Yadav
    Rakesh Yadav about 6 years
    Not a perfect one, but helped me to get the solution.
  • Prakash Palnati
    Prakash Palnati almost 6 years
    Works fine for me.
  • Alin
    Alin over 5 years
    I 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
    Sagar almost 5 years
    you can try for GradientDrawable bgShape = (GradientDrawable) bubble_text.getBackground().mutate(); bgShape.setColor(color); bgShape.setStroke(1, color);
  • Abhi S
    Abhi S about 2 years
    How to get that change color drawable shape as a Drawable?