Main Tutorials

Android ImageButton example

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.

Note
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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android_button" />
    
</LinearLayout>

3. Code Code

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

File : MyAndroidAppActivity.java


package com.mkyong.android;

import android.app.Activity;
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;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addListenerOnButton();

	}

	public void addListenerOnButton() {

		imageButton = (ImageButton) findViewById(R.id.imageButton1);

		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			   Toast.makeText(MyAndroidAppActivity.this,
				"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 – Android-ImageButton-Example.zip (28 KB)

References

  1. Android ImageButton example

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
KJ
10 years ago

Many Thanks!!!

Anurag
11 years ago
 public class Mp3Player extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
 ImageButton ib1=(ImageButton) findViewById(R.id.Playbutton);
 ImageButton ib2=(ImageButton) findViewById(R.id.Pausebutton);
 ImageButton ib3=(ImageButton) findViewById(R.id.Forwardbutton);
 ImageButton ib4=(ImageButton) findViewById(R.id.Rewindbutton);
 
 
 ib1.setOnClickListener(this);
 
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_mp3_player);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_mp3_player, menu);
        return true;
    }

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		switch(arg0.getId()) {
		case R.id.Playbutton:
			Log.d("MediaPlayer", "Play button was clicked");
			break;

		case R.id.Pausebutton:
			Log.d("MediaPlayer", "Pause button was clicked");
		default:
			break;
		}
		
	}
} 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/mp_img" />

    <ImageButton
        android:id="@+id/Playbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:src="@drawable/play" />

    <ImageButton
        android:id="@+id/Pausebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/Playbutton"
        android:layout_toRightOf="@+id/Playbutton"
        android:src="@drawable/pause" />

    <ImageButton
        android:id="@+id/Rewindbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/Pausebutton"
        android:src="@drawable/rewind" />

    <ImageButton
        android:id="@+id/Forwardbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Rewindbutton"
        android:layout_toLeftOf="@+id/Rewindbutton"
        android:src="@drawable/forward" />

</RelativeLayout>
 

Why is my application not running?

sss
11 years ago

Hi,
thanks for the article 🙂 I was wondering if I can do this without the xml part. Because i have an array of images and I need to put one of the images to the button. Do I have to create the array in xml or can I write it normally in java? I am quite begginer in xml and the concept of moving from xml to java and back.
Thanks 🙂

sizzatul manira
9 years ago

Can you please help me how to add but i want to different pages in one page of android recipe
Please I am new to android….

irfan kureshi
9 years ago

how to take a image button array…..plz give me example……

Nikola Pavlica
10 years ago

Thanks so much,it even works for AIDE!

harga terbaru
10 years ago

very helpful…thx

Gerardo Flores
10 years ago

When i make a imagebutton the image (png file) seems low quality and with a padding inside the button, there’s a way to make it exactly of the size of the png image and without that padding? Thank you so much.

Check out this website
10 years ago

Awesome blog! Is your theme custom made or did you download it from
somewhere? A design like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your design. Thanks a lot

Josh F.
10 years ago

This info saved me a lot of time. I was originally trying to assign a view with a drawable to a Button, but it obviously wasn’t working. So, thanks a ton for posting this example source code for an ImageButton.

Josh F.
10 years ago

This information saved me a lot of time. It worked perfectly. Thanks for posting this.

Chandra
10 years ago

Can you please help me how to add imagebutton dynamically in my activity…
Please I am new to android….

sanket mehta
11 years ago

hey dude u r awsome… I found it is better to search anything in ur blog instead of android blog…
Ver good keep working…

luki
11 years ago

good job… thanks for ur sample

Lane
11 years ago

Thanks for the quick instruction, it was much clearer than the Android for Abosolute Beginners book I was using (it’s not bad, just a little, uh, incomprehensible on this topic). Does this mean that each button on a form gets its own listener function? Does this apply to radio group buttons as well?

store
11 years ago

I’m really inspired along with your writing abilities as well as with the structure on your weblog. Is this a paid topic or did you customize it yourself? Anyway keep up the nice high quality writing, it is uncommon to look a nice blog like this one nowadays..

Lane
11 years ago
Reply to  store

You may want to delete this comment. Smells like spam.

android gadget
11 years ago

nice info..!!
See : http://www.allandroidgadget.com
For Android Gadget Review Portal
Thank’s…

khan
10 years ago
Reply to  android gadget

nice