o7planning

Android ImageButton Tutorial with Examples

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- Android ImageButton

In Android, ImageButton is a user interface control, which displays as a button with an image, and allows the user to click to perform an action.
ImageButton is a subclass of ImageView, so you can set an Icon for it by using the app:srcCompat attribute. Note that unlike ButtonImageButton is not possible to display text on it.

<ImageButton
    android:id="@+id/imageButton"
    app:srcCompat="@drawable/icon_feel_good"
    ... />
There are other types of buttons that you can take a look in the following articles:
As for Java code, there are several methods that help you set the Icon for ImageButton. All of them are inherited from ImageView.
  1. setImageBitmap(Bitmap bitmap)
  2. setImageDrawable(Drawable drawable)
  3. setImageIcon(Icon icon)
  4. setImageResource(int resId)
  5. setImageURI(Uri uri)

2- ImageButton Styles

The style attribute is an option of ImageButton. It permits you to set styles for the ImageButton. There are some styles available in Android library that may be ready to use.

<ImageButton
    android:id="@+id/imageButton"
    style="@android:style/Widget.Holo.ImageButton"
    app:srcCompat="@drawable/icon_feel_good"
    ... />
  • style="@style/Widget.AppCompat.ImageButton"
  • style="@android:style/Widget.Holo.ImageButton"
  • style="@android:style/Widget.ImageButton"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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=".MainActivity">

    <TextView
        android:id="@+id/textView11"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Widget.AppCompat.ImageButton (Default)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/imageButton11"
        style="@style/Widget.AppCompat.ImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView11"
        app:srcCompat="@drawable/icon_feel_good" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Widget.Holo.ImageButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageButton11" />

    <ImageButton
        android:id="@+id/imageButton12"
        style="@android:style/Widget.Holo.ImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView12"
        app:srcCompat="@drawable/icon_feel_good" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="Widget.ImageButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageButton12" />

    <ImageButton
        android:id="@+id/imageButton13"
        style="@android:style/Widget.ImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView13"
        app:srcCompat="@drawable/icon_feel_good" />

</androidx.constraintlayout.widget.ConstraintLayout>

3- ImageButton Event

Basically, both ImageButton and Button have the same events, which you can read the Button article to learn more about these events.
The events of ImageButton and Button:
  • button.setOnTouchListener(View.OnTouchListener)
  • button.setOnClickListener(View.OnClickListener)
  • button.setOnLongClickListener(View.OnLongClickListener)
  • button.setOnContextClickListener(View.OnContextClickListener);
  • ...

View more Tutorials: