Klaus Nji
Klaus Nji

Reputation: 18877

android ImageButton do not stretch image

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?

Upvotes: 5

Views: 4510

Answers (3)

arpit
arpit

Reputation: 1462

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" />

Upvotes: 0

Ajay Venugopal
Ajay Venugopal

Reputation: 1710

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.

Upvotes: 0

azerto00
azerto00

Reputation: 1000

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

Upvotes: 9

Related Questions