Android ImageButton Control


An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children. This shows a button with an image (instead of text) that can be pressed or clicked by the user.

Image Button

Android button style set

ImageButton Attributes

Following are the important attributes related to ImageButton control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

Inherited from android.widget.ImageView Class −

Attribute Description
android:adjustViewBounds Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
android:baseline This is the offset of the baseline within this view.
android:baselineAlignBottom If true, the image view will be baseline aligned with based on its bottom edge.
android:cropToPadding If true, the image will be cropped to fit within its padding.
android:src This sets a drawable as the content of this ImageView.

Inherited from android.view.View Class −

Attribute Description
android:background This is a drawable to use as the background.
android:contentDescription This defines text that briefly describes content of the view.
android:id This supplies an identifier name for this view,
android:onClick This is the name of the method in this View's context to invoke when the view is clicked.
android:visibility This controls the initial visibility of the view.


This example will take you through simple steps to show how to create your own Android application using Linear Layout and ImageButton.

Step Description
1 You will use Android studio IDE to create an Android application and name it as myapplication under a package com.example.myapplication as explained in the Hello World Example chapter.
2 Modify src/ file to add a click event.
2 Modify the default content of res/layout/activity_main.xml file to include Android UI control.
3 No need to define default constants in android, Android studio takes care of default constants.
4 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.myapplication/ This file can include each of the fundamental lifecycle methods.

package com.example.myapplication;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {
   ImageButton imgButton;
   protected void onCreate(Bundle savedInstanceState) {
      imgButton =(ImageButton)findViewById(;
      imgButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
   public boolean onCreateOptionsMenu(Menu menu) {
      /* Inflate the menu; this adds items to the action bar
      if it is present */
      getMenuInflater().inflate(, menu);
      return true;

Following will be the content of res/layout/activity_main.xml file −

<RelativeLayout xmlns:android=""
   xmlns:tools="" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <TextView android:text="Tutorials Point"
      android:layout_alignEnd="@+id/imageButton" />


Following will be the content of res/values/strings.xml to define these new constants −

<?xml version="1.0" encoding="utf-8"?>
   <string name="app_name">myapplication</string>
   <string name="action_settings">Settings</string>

Following is the default content of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
   android:versionName="1.0" >
      android:targetSdkVersion="22" />
      android:theme="@style/AppTheme" >
         android:label="@string/app_name" >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Let's try to run your myapplication application. I assume you had created your AVD while doing environment setup. To run the app from Android Studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −


The following screen will appear after ImageButton is clicked,It shows a toast message.

Toast Message


I will recommend to try above example with different attributes of ImageButton in Layout XML file as well at programming time to have different look and feel of the ImageButton. Try to make it editable, change to font color, font family, width, textSize etc and see the result. You can also try above example with multiple ImageButton controls in one activity.