Search code examples
androidshapesstroke

Is there a way to set stroke behind shape


When I add stroke as shown below it is always half inside a shape and half outside. I want it to be only outside (or behind the shape), because I'm using semitransparent stroke and it's ugly when it covers shape. Sorry about my English, I hope you understand what I meant and what I want.

<?xml version="1.0" encoding="utf-8"?>
<inset android:inset="16.0dip"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <shape android:shape="rectangle">
            <corners android:radius="10.0dip" />
            <solid android:color="?android:colorBackground" />
            <stroke android:width="18dp" android:color="#40ffffff" />
        </shape>
</inset>

Solution

  • Layer two different shapes using a layer-list.

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="10dip" />
                <stroke android:width="18dp" android:color="#40ffffff" />
            </shape>
        </item>
        <item
            android:top="8dip"
            android:right="8dip"
            android:bottom="8dip"
            android:left="8dip">
            <shape android:shape="rectangle">
                <corners android:radius="10dip" />
                <solid android:color="?android:colorBackground" />
            </shape>
        </item>
    </layer-list>