2
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="@dimen/radius_default"/>
    <solid
        android:color="@color/alpha"/>

    <stroke
        android:width="1dp"
        android:color="@color/red"/>

</shape>

I want to change stroke's color at runtime.

I used this code but it didn't work.

Drawable drawable = getResources().getDrawable(R.drawable.border_red);
        drawable.setColorFilter(Color.BLUE), PorterDuff.Mode.SRC_ATOP);
        container.setBackground(drawable);

How can I change stroke's color?


        float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
        RectF inset = new RectF(6, 6, 6, 6);
        float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
        ShapeDrawable biggerCircle = new ShapeDrawable(new RoundRectShape(outerR,inset, innerR));
        biggerCircle.setIntrinsicHeight( 60 );
        biggerCircle.setIntrinsicWidth( 60);
        biggerCircle.setBounds(new Rect(30, 30, 30, 30));
        biggerCircle.getPaint().setColor(Color.BLUE); 
        border.setBackgroundDrawable(biggerCircle);

i do not refactoring yet, but this code is work for me. thank you

jihoon kim
  • 1,270
  • 1
  • 16
  • 22

3 Answers3

4

Run time shapes are not looking good as much as from xml, But we can create shapes and we can change it's background colors as well as its border color.

I create one method it may help full to you

   public static LayerDrawable getDrawable(Shape shape, int colorBGNormal, int colorBorderNormal, int defaultStrokeWidth) {

    Drawable[] layers = new Drawable[2];
    ShapeDrawable background = new ShapeDrawable();
    ShapeDrawable backgroundBorder = new ShapeDrawable();

    background.setShape(shape);
    background.getPaint().setColor(colorBGNormal);
    background.getPaint().setAntiAlias(true);

    backgroundBorder.setShape(shape);
    backgroundBorder.getPaint().setColor(colorBorderNormal);
    backgroundBorder.getPaint().setStyle(Style.STROKE);
    backgroundBorder.getPaint().setStrokeWidth(defaultStrokeWidth);
    backgroundBorder.getPaint().setAntiAlias(true);

    layers[0] = background;
    layers[1] = backgroundBorder;

    return new LayerDrawable(layers);
}
Ravi Jaggarapu
  • 627
  • 3
  • 10
3

this one work for me and gave me round shape with color

 ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
                biggerCircle.setIntrinsicHeight( 60 );
                biggerCircle.setIntrinsicWidth( 60);
                biggerCircle.setBounds(new Rect(30, 30, 30, 30));
                biggerCircle.getPaint().setColor(Color.parseColor(first));//you can give any color here
                holder.firstcolor.setBackgroundDrawable(biggerCircle); 
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
1

First of all get the Drawable using getbackground().

Drawable background = imageView.getBackground();


if (background instanceof ShapeDrawable) {
    // cast to 'ShapeDrawable'
    ShapeDrawable shapeDrawable = (ShapeDrawable)background;
    shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
    // cast to 'GradientDrawable'
    GradientDrawable gradientDrawable = (GradientDrawable)background;
    gradientDrawable.setColor(getResources().getColor(R.color.colorToSet));
}
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53