Android button

Download android button customization & usage sample

In android, we can use 3 types of button:

  1. Text based button
  2. Text & Image based button
  3. Image based button

In this tutorial, we are going to discuss first 2 aspects of Android Button.

Android button usage?

  • Text based button usage:

  • Text & Image based button usage:

    android:drawableLeft, android:drawableRight, android:drawableTop and android:drawableBottom attribute of button can be used to set icon of button in appropriate direction.

How to make borderless button in Android?

Borderless has all the properties of Button except that they don't have button.
To create a borderless button, apply the borderlessButtonStyle attribute to the button as follows:

How to style android button?

We can add html like style in Android button, that can define multiple button properties like font, textStyle, textSize, textColor, background etc. An example of defining style is given below:

res -> values -> styles.xml

Next step is to apply this theme to our android button.

How to add custom background in android button?

We can provide simple bitmap or color as background of Android Button but we should avoid it. Instead of supplying simple bitmap or color we should use selector to define state list for our button that can use simple bitmap as well as color.

To create a state list drawable for your button background create three bitmaps for the button background that represent the normal, pressed, and focused button states. Place the bitmaps into the res/drawable/ directory of your project

Create a new XML file in the res/drawable/ directory and name it button_selector.xml. Insert the following XML:

res -> drawable -> button_selector.xml

Last step is to set background of desired button as follows:

How to handle click event of android button?

There are several way to handle click event of an android button. We are going to demonstrate 4 ways of handling click event of an Android button:

  1. A simple way to handle click event of a button is to define onClick attribute of button as follows:

    Now create a method named handleBorderLessButtonClick inside Activity class that hold the button layout. This method must be public, Return void and accept View as a parameter as follows:

  2. We can also declare the click event handler programmatically rather than in an XML layout. To declare the event handler programmatically, create an View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) as follows:

  3. Third way to handle click event of a button, implements View.OnClickListener, and override onClick method as follows:

    Next step is to assign onClickListener for the button as follows:

  4. Other way to handle onClick event of a button is to create a separate class that implements View.OnClickListener as follows:

    Next step is to assign onClickListener for the button as follows:

Example of Android Button

Let's quickly implement a sample for Android Button, which demonstrate whatever we discussed about Android Button.


Android button

Step 1: Create a new project in Android Studio and name it Android Button.

Step 2: Open res -> layout -> activity_main.xml and add following code


Step 3: Open res -> drawable and place 3 different images for different states of button. Images of my sample is button_normal.png, button_focussed.png, button_pressed.png

Step 4: Create res -> drawable -> button_selector.xml and add following code


Step 5: Open res -> values -> styles.xml and replace its code with below given code of styles.xml


Step 6: Create a class class that implements View.OnClickListener interface. Replace the code of with below given code:

Step 7: Add below given code to your

Step 8: Clean and run the project to see the result.

Share & Like

Tutorial topics

About is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.