2015年10月29日 星期四

Change the button background

1.at drawable create a xml file.eg custom_button_background.xml

2.define the color in the values/colors.xml

3.in custom_button_background.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/orangeDark" android:state_pressed="true"></item>
    <item android:drawable="@color/orange"></item>

</selector>


android:state_pressed="true" define when press, the color change to this color.

4.Use


<Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_button_background"
        android:padding="5dp"
        android:text="@string/materiallize"
        android:textColor="@color/textColor" />


different state:

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList