Skip to content

Instantly share code, notes, and snippets.

@JonathanNye
Created March 28, 2012 19:55
Show Gist options
  • Save JonathanNye/2229968 to your computer and use it in GitHub Desktop.
Save JonathanNye/2229968 to your computer and use it in GitHub Desktop.
Simple bottom "stroke" XML drawable for Android View backgrounds
<!-- This produces a 1.5dp stroke along the bottom of a View. It works by drawing the stroke color as the background of the View, then the background color on top, offset up by 1.5dp. Unfortunately, this approach
only works if you have solid color backgrounds. For transparency, you'll have to use a 9patch. -->
<?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="STROKE_COLOR_HERE"/>
</shape>
</item>
<item android:bottom="1.5dp">
<shape android:shape="rectangle">
<solid android:color="BACKGROUND_COLOR_HERE"/>
</shape>
</item>
</layer-list>
@rsyrysy
Copy link

rsyrysy commented Apr 8, 2014

great
can you explain me stroke in xml

@wansong
Copy link

wansong commented Dec 7, 2015

cool!

@beta
Copy link

beta commented Dec 14, 2015

It works for me and there's no need for a 9-patch drawable.

@rsyrysy there's not actually any stroke or line in the XML. The layer-list drawable is drawn one over another, with the last one on the top. So it's basically two rectangles, and the second one leaves some space on the bottom to show the "stroke" color of the first one.

@belinwu
Copy link

belinwu commented May 4, 2016

What about the GPU Overdraw?

@abhilash007
Copy link

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

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

this is not working properly

@yesitskev
Copy link

@abhilash007 some vendors change the value of transparent. Rather use your own value

@imansuts
Copy link

if I want to make transparent TextView with bottom stroke line then how will I make it?

@mochadwi
Copy link

awesome thanks a lot @JonathanNye

@mochadwi
Copy link

It works for me and there's no need for a 9-patch drawable.

@rsyrysy there's not actually any stroke or line in the XML. The layer-list drawable is drawn one over another, with the last one on the top. So it's basically two rectangles, and the second one leaves some space on the bottom to show the "stroke" color of the first one.

nice explanation thanks

@huSSooxXx
Copy link

Perfect. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment