Sunday, January 11, 2015

How to set Button pressed state background color in Android

Button background color drawable state
activity_main.xml code snippet

<Button
 android:id ="@+id/push_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Button Background Color - Drawable State"
 android:background="@drawable/button_bg_color_with_state"
 android:padding="15dp"
 />
res/drawable/button_bg_color_with_state.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--button default background color-->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FF8C00" />
            <corners android:radius="5dp"/>
        </shape>
    </item>

    <!--button focused state background color-->
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#E9967A" />
            <corners android:radius="5dp"/>
        </shape>
    </item>

    <!--button pressed state background color-->
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#8FBC8F" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
More android examples