Search code examples
androiddrawable

How to add a stroke to ShapeDrawable


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


Solution

  • 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?