In Android, you can use “android.widget.ImageButton” to display a normal “Button“, with a customized background image.

In this tutorial, we show you how to display a button with a background image named “android_button.png“, when user click on it, display a short message. As simple as that.

You may also like this Android ImageButton selector example, which allow to change button’s images depends on button states.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Add Image to Resources

Put image “android_button.png” into “res/drawable-?dpi” folder. So that Android know where to find your image.

2. Add ImageButton

Open “res/layout/main.xml” file, add a “ImageButton” tag, and defined the background image via “android:src“.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:src="@drawable/android_button" />

3. Code Code

Here’s the code, add a click listener on image button.

File :

import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
	ImageButton imageButton;
	public void onCreate(Bundle savedInstanceState) {
	public void addListenerOnButton() {
		imageButton = (ImageButton) findViewById(;
		imageButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				"ImageButton is clicked!", Toast.LENGTH_SHORT).show();

4. Demo

Run the application.

1. Result, a button with a customized background image.

android imagebutton demo1

2. Click on the button, a short message is displayed.

android imagebutton demo2

Download Source Code

Download it – (28 KB)


  1. Android ImageButton example
Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts