Difference between a clickable ImageView and ImageButton

AndroidAndroid ImageviewImagebutton

Android Problem Overview


I'm just wondering if there is any significant difference between an ImageView that's set to be clickable, compared with an ImageButton?

Is there any reason for using one over the other? Is there any restriction on the drawable for an ImageButton that leaves ImageView as the only possible option?

Will I possibly lose any functionality of a button if I opt for a clickable ImageView over ImageButton?

Android Solutions


Solution 1 - Android

There's no differences, except default style. ImageButton has a non-null background by default.

EDIT: Also, ImageButton.onSetAlpha() method always returns false, scaleType is set to center and it's always inflated as focusable.

Here's ImageButton's default style:

 <style name="Widget.ImageButton">
     <item name="android:focusable">true</item>
     <item name="android:clickable">true</item>
     <item name="android:scaleType">center</item>
     <item name="android:background">@android:drawable/btn_default</item>
 </style>

Solution 2 - Android

ImageButton is inherited from ImageView

public class ImageButton extends ImageView {
public ImageButton(Context context) {
    this(context, null);
}

public ImageButton(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}

public ImageButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFocusable(true);
}

@Override
protected boolean onSetAlpha(int alpha) {
    return false;
}

@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
    super.onInitializeAccessibilityEvent(event);
    event.setClassName(ImageButton.class.getName());
}

@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
    super.onInitializeAccessibilityNodeInfo(info);
    info.setClassName(ImageButton.class.getName());
}

as @Micheal describe i just add details to his answer

Solution 3 - Android

The effect of a button click when I click is there for imagebutton but not for imageView.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyjwView Question on Stackoverflow
Solution 1 - AndroidMichaelView Answer on Stackoverflow
Solution 2 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 3 - AndroidPrashanth DebbadwarView Answer on Stackoverflow