Android Bordered Button

Daniel Black
1 min readDec 28, 2016

--

The goal here is very simple. Create a transparent button that has a colored border.

You need to create a drawable for the <Button> that defines the transparent background and border.

transparent_bg_bordered.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!-- background -->
<solid android:color="@color/transparent" />
<!-- border -->
<stroke android:width="2dp" android:color="@color/blue" />

<!-- corner roundness -->
<corners android:radius="2dp" />
</shape>
</item>
</selector>

Android buttons by default have a padding around them, so your button height will appear larger than a default Android button. Create another drawable that provides some padding to match the default (~6dp).

transparent_bg_bordered_button.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="6dp"
android:drawable="@drawable/transparent_bg_bordered"
android:top="6dp" />
</layer-list>

Now, put it all together and voila! Hope this helps!

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_bg_bordered_button"
android:text="Voila"
android:textColor="@color/blue" />

--

--