1

I need to set the border colour and fill colour of a shapedrawable to different colours. How can I set both colours and have the shape as fill and stroke?

Say I have this:

        ShapeDrawable border = new ShapeDrawable(new RectShape());

        border.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        border.getPaint().setColor(Color.WHITE);
        border.getPaint().setStrokeColour(Color.BLACK); //???????? How to do

Edit: My question is not a duplicate of Android ShapeDrawable set Background and Border programmatically because that question's only answer is unclear and does not help me.

null
  • 171
  • 1
  • 9
  • Possible duplicate of [Android ShapeDrawable set Background and Border programmatically](https://stackoverflow.com/questions/31103974/android-shapedrawable-set-background-and-border-programmatically) – Vidhi Dave Jun 18 '18 at 10:50
  • https://stackoverflow.com/a/17825210/8089770 – Vidhi Dave Jun 18 '18 at 10:50

2 Answers2

0

You might want to define your drawable in XML instead. It would look something like this:

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

<solid android:color="@color/WHITE"/>

<stroke
    android:width="2dp"
    android:color="@color/BLACK" />

Lars
  • 710
  • 5
  • 19
0
you might define an xml like below and use it where you need:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="7dp"
        android:topRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp" />
    <solid android:color="@android:color/white" />
    <stroke android:width="1dip" android:color="#c9d1d7"/>
</shape>
dariush
  • 216
  • 3
  • 10