5

How can I prevent the ImageButton from stretching the image?

Using this snippet:

 <ImageButton
                android:src="@drawable/prediction"
                android:background="?android:attr/selectableItemBackground"
                android:gravity="center_horizontal"
                android:scaleType="centerInside"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:contentDescription="some description"
                android:text="@string/predict"
                local:MvxBind="Click ExecuteQueryCmd" />

I get the following effect:

enter image description here

Image is 64x64 png.

All I want is for the ImageButton to respect native width and length, hence resolution, of the image it is being assigned. Recommendations based on a similar post is to use a Button, set its background to an image then use custom selectors but this sounds like a hack. Or is this the proper way to do this?

Community
  • 1
  • 1
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185

3 Answers3

9

It work for me with

  <ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:contentDescription="some description"
    android:scaleType="centerInside"
    android:src="@drawable/ci_icon"
    android:text="@string/predict"
    local:MvxBind="Click ExecuteQueryCmd" />

Put your image in android:src attribute

azerto00
  • 1,001
  • 6
  • 18
  • Just did and image still stretches. Frustrating really how difficult it is to do such simple things on Android. Also note that I cannot use @android:color/transparent" because button no longer changes color when clicked. – Klaus Nji Nov 19 '13 at 22:43
0

Set the width of the ImageButtons to fill_parent and use scaletype fitStart for the images that hug the left margin, and fitEnd for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.

Ajay Venugopal
  • 1,544
  • 1
  • 17
  • 30
0

Do this:

<ImageButton
                android:src="@drawable/prediction"
                android:background="?android:attr/selectableItemBackground"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:contentDescription="some description"
                android:text="@string/predict"
                local:MvxBind="Click ExecuteQueryCmd" />
arpit
  • 1,462
  • 1
  • 12
  • 12