VinayChoudhary99
VinayChoudhary99

Reputation: 914

Border drawable for a view in Android with margins on left or right

I need to have a border drawable for a view.. I did it as below. But I also need to have some margins on Left/Right side.

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

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>

    <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <stroke
                android:width="2dp"
                android:color="@color/border_color"/>
        </shape>
    </item>
</layer-list>

I have tried a bit with inset, but not able to have desired result.

How do I do that?

Upvotes: 2

Views: 6061

Answers (1)

Mike
Mike

Reputation: 4570

Insets should work but probably you didn't find the right solution using them. Here is a working example for what you need:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="16dp"
    android:insetRight="16dp">
    <layer-list>      
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@android:color/transparent"/>
            </shape>
        </item>

        <item
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape>
                <stroke
                    android:width="2dp"
                    android:color="@color/border_color"/>
            </shape>
        </item>
    </layer-list>
</inset>

Upvotes: 11

Related Questions