Search code examples
androidxmlviewborderdrawable

Set border for view with an icon in the corner


I would like to add a border to view which to have an icon in the corner like this:

enter image description here

I write a drawable with border:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
<stroke
    android:width="2dp"
    android:color="#F62626"
    android:dashWidth="3dp"
    android:dashGap="7dp" />

but I can't add icon in the corner. Thanks for every answersenter image description here.


Solution

  • This is possible using layer-list, drawable and attributes like adroid:top, left, right, bottom. Something like this:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/ic_lock"
            android:top="-10dp"
            android:left="-10dp"/>
        <item
            android:left="10dp"
            android:top="10dp"
            android:bottom="10dp"
            android:right="10dp">
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="2dp"
                android:color="#F62626"
                android:dashWidth="3dp"
                android:dashGap="7dp" />
            </shape>
        </item>
    </layer-list>
    

    The other way is to use ConstraintLayout and put there icons as separate views.