Monday, January 12, 2015

How to create a transparent background Button with border in Android

Button transparent background with border
activity_main.xml code snippet

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="left"
    android:layout_margin="15dp"
    android:background="#9370DB"
    tools:context=".MainActivity"
    >
    <!--@null remove background and make the button transparent-->
    <Button
        android:id ="@+id/push_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Background Transparent"
        android:background="@drawable/button_bg_transparent"
        android:padding="15dp"
        />
</LinearLayout>
res/drawable/button_bg_transparent.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <!--apply button background transparent, full opacity-->
        <solid android:color="#00ffffff"/>
        <!--make button border solid color, nontransparent-->
        <stroke android:color="#483D8B" android:width="2dp"/>
        <corners android:radius="2dp"/>
    </shape>
</item>
</selector>
More android examples