ImageButton is used to get a response from user onClick event. OnClick event is called through setOnclickListener and followed by OnClickListener with button. The main difference between Button and ImageButton is to link multiple images and change background with the multiple images. There is another way to declare Image button, in which we declare example.xml file 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 : 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 of 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 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  ImageButton in Android

  Modified On Sep-07-2019 12:17:58 AM

Leave Comment