Using Button Selectors in Android

If you do somethings in Buttons like background changing, stroke color means, Just follow up the xml.


Button_Selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_pressed_bg" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/btn_pressed_bg" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/btn_pressed_bg" />
    <item android:state_enabled="false" android:drawable="@drawable/btn_disabled"></item>
    <item android:drawable="@drawable/btn_normal_bg" />
</selector>

btn_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="5dp" />

    <stroke
        android:width="2dp"
        android:color="#00ff00" />

    <solid android:color="@color/white" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>
btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="5dp" />

    <stroke
        android:width="1dp"
        android:color="#00B800" />

    <solid android:color="@color/white" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>
btn_disabled.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="5dp" />

    <stroke
        android:width="2dp"
        android:color="#00ff00" />

    <solid android:color="@color/white" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>

colors.xml
    <color name="green_pressed">#00B800</color>
    <color name="green_normal">#00B800</color>
    <color name="green_disabled">#00FF00</color>

Comments

Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.