---
title: "ImageButton in Android"  
description: "ImageButton is used to get a response from user onClick event. OnClick event is called through setOnclickListener and followed by OnClickListener with"  
author: "Nitish Kesarwani"  
published: 2017-12-30  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12753/imagebutton-in-android  
category: "android apps"  
tags: ["android", "android activity", "onclicklistener"]  
reading_time: 3 minutes  

---

# ImageButton in Android

[ImageButton](https://www.mindstick.com/forum/2276/how-to-add-an-imagebutton-to-label) is used to get a response from user onClick event. OnClick event is called through setOnclickListener and followed by [OnClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach) with button. The main [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between Button and ImageButton is to link multiple images and change [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) with the multiple images. There is another way to declare [Image button](https://www.mindstick.com/forum/1535/css-image-button), in which we declare example.[xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) in res\drawable directory to show the ImageButton with different events using selector tag. Item is necessary tag because it define the element in order.

## Example

```
  <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true" //Pressed Event           android:drawable="@drawable/Image1" />     <item android:state_focused="true" //focused Event           android:drawable="@drawable/image2" />     <item android:drawable="@drawable/image3" /> //Default Event </selector>
```

##### Attributes

• **android:****ContentDescription** :- It defines text which covers brief description about the content of the view.

• **android:adjustViewBounds:** its value must be true to adjust the bounds ImageView and must be retained in a ratio which is drawable.

• **android:baseline** :It is adjustable with the baseline of the view.

• **android:cropToPadding** :It can be fitted in the screen when cropping is done by this attribute.

• **android:baselineAlignBottom:** if true, the ImageView Is aligned with the baseline.

• **Android:****[visibility](https://www.mindstick.com/blog/301300/how-to-improve-your-visibility-in-zero-click-search-results)** : it provides initial visibility of ImageView control.

• **Android:****src** : It is very similar to html , which is used define the source of image.

##### Methods

• **getAccessibilityClassName()** : It returns the [accessibility](https://www.mindstick.com/interview/209/describe-the-accessibility-modifier-protected-internal) of [class name](https://www.mindstick.com/forum/12871/how-to-get-class-name) of this object and its return type is CharSequence.

• **onResolvePointerIcon() :** This method return pointer icon is for the Motion event which is defined in directory. The default [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) does not care about the location or event types, but some controls use WebView.

##### Android_Mainfest.xml

```
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.mapsample.msclient009.imgbtn">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
```

## Activiy_Main.xml

```
<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.mapsample.msclient009.imgbtn.MainActivity"><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <ImageButton        android:id="@+id/imgbtn"        android:padding="0dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        app:srcCompat="@drawable/banana" /></LinearLayout>    <TextView        android:id="@+id/tview"        android:background="#f01"        android:textColor="#F9f9f9"        android:textSize="25dp"        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="ImageButton "        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" /></LinearLayout>
```

##### Activity_Main.java

```
package com.mapsample.msclient009.imgbtn;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;public class MainActivity extends AppCompatActivity {ImageButton img_btn;TextView tv;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate (savedInstanceState);        setContentView (R.layout.activity_main);        img_btn =(ImageButton)findViewById (R.id.imgbtn);        tv=(TextView)findViewById (R.id.tview);        img_btn.setOnClickListener (new View.OnClickListener ( ) {            @Override            public void onClick(View v) {                tv.setText ("Button is click");                img_btn.setBackgroundResource (R.drawable.cherry);                tv.setBackgroundResource (R.drawable.banana);            }        });    }}
```

![ImageButton in Android](https://www.mindstick.com/mindstickarticle/b7a52c4c-a2c7-43e7-9d64-b7b6a743d641/images/bdbd709c-022f-4e4a-ab60-e0ae5fe7ad28.png) ![ImageButton in Android](https://www.mindstick.com/mindstickarticle/b7a52c4c-a2c7-43e7-9d64-b7b6a743d641/images/5e94f2f3-7b31-4790-9b6b-89819eabe962.png)\

---

Original Source: https://www.mindstick.com/articles/12753/imagebutton-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
