Sunday, August 12, 2012

How to create custom background button

1. Create a new xml in drawable folder (customize_button.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/ic_launcherweb" />
    <item android:state_focused="true"
          android:state_pressed="true"
          android:drawable="@drawable/ic_launcher" />
    <item android:state_focused="false"
          android:state_pressed="true"
            android:drawable="@drawable/ic_launcherweb" />
    <item android:drawable="@drawable/ic_launcher"
           android:state_focused="false"
           android:state_pressed="false"/>
</selector>

2. now add a new line in your main.xml (android:background="name_of_background"

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="82dp"
        android:text="customized button"
        android:background="@drawable/customize_button" />

now you will have your own customized background button

No comments:

Post a Comment