161

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?

Axxiss
  • 4,759
  • 4
  • 26
  • 45
yjw
  • 3,394
  • 4
  • 20
  • 20

4 Answers4

168

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>
Michael
  • 53,859
  • 22
  • 133
  • 139
  • 1
    Thanks for your answer. You gave me more than I spotted when going through the code myself. I guess end of the day, selection between the 2 will depend on how much of the default properties you can use without any customization. – yjw May 01 '11 at 15:24
  • You're welcome! Yeah, there isn't much difference indeed, so the choice is between button and not button, I think. – Michael May 01 '11 at 16:29
  • 20
    Actually in my experience another difference between the two is that if you want to put a clickable button into a ListView´s cell while maintaining the cell itself as clickable you are much better off with an Imageview. EditTexts and ImageButtons seem to consume the touch event when TextViews and ImageViews don´t. – Ernir Erlingsson Nov 29 '12 at 11:36
  • I had used clickable ImageView but as per new requirement I have to change them to ImageButton but the image used gets Inflated. How can i avoid the image inflation in ImageButton ? – codeRider Mar 16 '15 at 10:58
  • @codeRider I'm not sure I understand what you mean. If it's positioning differs from what it was in `ImageView` you can and specify `scaleType` explicitly. – Michael Mar 16 '15 at 11:37
26

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

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
8

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

Prashanth Debbadwar
  • 1,047
  • 18
  • 33
0

If you want, you can give the button clic effect as a background. ImageButton has an effect by default.