Creating custom buttons in android applications

Posted on Updated on

Dont you feel bored of using the default button background in your Android application. Here I am going to explain how to add custom background for buttons in your Android applications.

  1. Create a new Android project in Eclipse.
  2. Add a button in your layout XML  file.
  3. Create a xml file in the drawable folder. For example res->drawable-> mybutton.xml
  4. In the drawable folder add three PNG files which denotes three different states for your button such as

               The first image is normal.png, the second one is pressed.png and the last one is focussed.png

         5. The above shown png files shows how the button should change its state as the user presses the button.
         6. Now in the mybutton.xml file add the following code
<?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/normal" />
  <item android:state_focused="true"
  android:state_pressed="true"
  android:drawable="@drawable/pressed" />
  <item android:state_focused="false"
  android:state_pressed="true"
  android:drawable="@drawable/focussed" />
  <item android:drawable="@drawable/normal" />
  </selector>

        7. Now in the main layout file where the button has been created add the button background as                              

<Button
  android:id="@+id/button1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/mybutton"
  android:text="CLICK ME" />

8. Once the background has been added to the button the new theme will be applied.

4 thoughts on “Creating custom buttons in android applications

    Hiram said:
    November 25, 2011 at 5:20 am

    Hey, great article. Bookmarked the site 😉

    saving private sheep apk said:
    April 29, 2012 at 5:28 am

    Creating custom buttons in android applications DEXTERS ANDROLAB I was suggested this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are wonderful! Thanks! your article about Creating custom buttons in android applications DEXTERS ANDROLABBest Regards Veronica

    mohamed said:
    June 4, 2012 at 8:09 pm

    thank you

    Zubair said:
    January 3, 2013 at 5:50 pm

    I was looking for some guide to use scalable & custom android buttons.
    and this post is quite useful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s