How to add a stroke to ShapeDrawable

10,599

Solution 1

It looks like this person struggled with the same issue and the only way they found was to subclass the ShapeDrawable:

Trying to draw a button: how to set a stroke color and how to "align" a gradient to the bottom without knowing the height?

Solution 2

The ShapeDrawable does not allow you to easily draw a stroke around it. If you really want then this would be a nice place to look at.

OR

You could use a GradientDrawable

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);

(PS: This is given as a comment in the same page!)

Share:
10,599
JustMe
Author by

JustMe

Updated on June 04, 2022

Comments

  • JustMe
    JustMe about 2 years

    Hi I want to create a shape drawable and fill it with gradient color with white stroke here is my code

        ShapeDrawable greenShape = new ShapeDrawable(new RectShape());
        Shader shader1 = new LinearGradient(0, 0, 0, 50, new int[] {
                0xFFBAF706, 0xFF4CD52F  }, null, Shader.TileMode.CLAMP);
        greenShape.getPaint().setShader(shader1);
        greenShape.getPaint().setStrokeWidth(3);
        greenShape.getPaint().setColor(Color.WHITE);
        greenShape.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);`
    

    The problem is the rectangle appears with gradient fill but without stroke

  • JustMe
    JustMe about 13 years
    I think it's a bug on Android SDK
  • android developer
    android developer almost 7 years
    How do I make it OvalShape? Meaning a filled circle, with a stroke of another color?