1

I'm trying to change the color of the stroke of a Drawable that I use as a background.

Here is my drawable:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/colorPrimary" />
            </shape>
        </item>

        <item
            android:id="@+id/item_border_drawable"
            android:left="16dp"
            android:right="16dp"
            android:top="16dp"
            android:bottom="16dp">
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
                <!-- This is the stroke you want to define -->
                <stroke android:width="3dp"
                    android:color="@color/itemLegendary"/>

                <!-- Optional, round your corners -->
                <corners android:bottomLeftRadius="0dp"
                    android:topLeftRadius="5dp"
                    android:bottomRightRadius="5dp"
                    android:topRightRadius="0dp" />
            </shape>
        </item>
</layer-list>

As you can see I use an item Rectangle to have a Background color, then another rectangle on top that draws a border around it but with 16dp of margin all around.

I want to change the color of the stroke in the MainActivity, without affecting the padding and background-color.

I tried to do as mentionned here: Android change color stroke (border) programmatically However it seems like it doesnt "change the color" but replace the whole Drawable and I don't want to have to give the information of the padding etc... once again.

Could you guys please help me to easily change the color of the border?

Regards.

Daniel Morena
  • 101
  • 1
  • 2
  • 5

0 Answers0