Jan Tacci
Jan Tacci

Reputation: 3201

ImageButton Stretching Background Image

I am trying to create an ImageButton with no border but I am running into an issue with the sizing of the image button.

Using Eclipse ADT I drag an ImageButton to the layout and select my background image. The image button appears as follows:

initial image button

As you can see there is a border between the background image and the image button perimeter which I would like to get rid of. To do so, I set the padding property to 0dp. The image button now looks like this:

image button after setting padding to 0dp

Finally, I set the scale type to fitXY:

image button after setting scale type

Notice that the image is stretched and is no longer the size of the original image. (I want the ImageButton size to be exactly the same size as the background image.)

Here is a snapshot of all 3 images next to each other to make the difference more clear:

all image buttons

I have tried re-sizing the image, setting the max size properties, etc. but nothing seems to prevent the image button from stretching the background image.

How do I prevent the image from stretching like it is?

UPDATE

I also tried setting scale type to matrix but here is what happened:

image button with scale type matrix

Upvotes: 3

Views: 4684

Answers (4)

mxmolins
mxmolins

Reputation: 289

You can set android:background="@null"

Upvotes: 0

Streets Of Boston
Streets Of Boston

Reputation: 12596

Use an ImageView instead and set it to be clickable. Set the scaleType to matrix (which will cause the ImageView to be as big as the image inside of it).

The problem with ImageButton is that it adds a background that causes padding, etc.

Upvotes: 0

Pierre Goldstein
Pierre Goldstein

Reputation: 1

Maybe this might help, but I also know its best practice to make different sized images from android development.

https://developers.google.com/appengine/docs/java/images/overview

Upvotes: 0

Anton-M
Anton-M

Reputation: 1111

Just set your image as android:background for button and set layout_height and layout_width for button to wrap_content

Upvotes: 1

Related Questions