IntoTheDeep
IntoTheDeep

Reputation: 4118

Android set radius programatically to ShapeDrawable

Can I set a radius programmatically to a ShapeDrawable?

Upvotes: 0

Views: 1559

Answers (2)

user7771871
user7771871

Reputation:

You can do it like this:

public static void customView(View v, int backgroundColor, int   borderColor)
{
  GradientDrawable shape = new GradientDrawable();
  shape.setShape(GradientDrawable.RECTANGLE);
  shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
  shape.setColor(backgroundColor);
  shape.setStroke(3, borderColor);
  v.setBackgroundDrawable(shape);
}

You can use this function throughout your app and can put border and background color of your choice.

Upvotes: 6

Divyesh Patel
Divyesh Patel

Reputation: 2576

try this:

        LayerDrawable bgDrawable = (LayerDrawable) btnCallnow.getBackground();
        final GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.shape_id);
        shape.setCornerRadius(5.0f);

Upvotes: 1

Related Questions