Monday, January 12, 2015

How to create a rounded corners Button with gradient background in Android

activity_main.xml code snippet

<Button
 android:id ="@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Rounded Corners Gradient Button"
 android:background="@drawable/button_rounded_corners_gradient"
 android:padding="15dp"
 />
res/drawable/button_rounded_corners_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <!--make a gradient background-->
        <gradient
            android:type="linear"
            android:startColor="#556B2F"
            android:endColor="#BDB76B"
            android:centerColor="#ffffff"
            android:angle="90"
            android:gradientRadius="90"
            />
        <!--apply a border around button-->
        <stroke android:color="#006400" android:width="2dp" />
        <!-- make the button corners rounded-->
        <corners android:radius="25dp"/>
    </shape>
</item>
</selector>
More android examples