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:
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:
Finally, I set the scale type to fitXY:
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:
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:
Just set your image as android:background for button and set layout_height and layout_width for button to wrap_content
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
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.
You can set
android:background="#null"
Related
I know that the question has been asked a lot, but not this case.
How can I make the ImageButton area follows the size of the image source of the ImageButton (the blue rectangle)? I wont't set fixed values for the width and height.
Is that possible?
1- Normal condition. 2- When I scale the image
Another description of the problem
I think this is what you need android:adjustViewBounds="true"
I have some problems resizing ImageViews and ImageButtons.
Let's say that I have a Layout that has a rectangular shape (I don't want to know if it is a horizontal or vertical rectangle) and a ImageButton that contains a transparent background and as ImageResource a square image.
I want to keep the button square, so I use setScaleType(ScaleType.FIT_CENTER) to stretch the button. It works well.
The problems come when the button needs to be REDUCED to fit the rectangular layout, instead of stretched: in that case, the image is reduced correctly, but the space reserved in the layout is the one that would be reserved by the image if I hade made it crop.
This is what I think that happens:
the image is put in the layout
the space in the layout is reserved
AFTER THIS the image is resized
if the space asked is increased, the layout is enlarged, otherwise nothing is done
as a consequence in the layout the image results rounded by A LOT of empty space if the image needed to be reduced.
The classical problem is: I have a layout that should contain one row with - say - six square buttons. IF the button size is larger than the height of the Horizontal LinearLayout, the buttons end to be distantiated with a lot of empty space, instead of touching them.
I tried using fixed sizes for the images, to force them resize before putting them in the layout, but this is not a solution for me. First of all I want it to be dynamic (ie: the layout could change size during the app lifetime and the images should follow that). Second of all, when I put the image into the layout it can easily happen that the layout is not set yet and its size returns zero.
Can anyone help me?
Thank you.
Just add the attribute
android:adjustViewBounds="true"
to your image view element in your layout. You can have a look to the post Unwanted padding around an ImageView
I put a background image to my Relative layout using android:background="#drawable/rajat2", where rajat2 is an image in all the drawable folders.
It sets the background to my layout but the background comes stretched to the whole screen.
I've tried putting the wallpaper through java code too but it still behaves the same!
If your drawable size is not exactly the screen size, the background will be stretched to the entire screen size. Consider creating the drawable for each type of screen size so it doesn't appear stretched.
If you don't want the image to take up the entire screen size, consider using an ImageView instead, and placing that inside your layout.
use draw9patch for the background image. it well help you.
If You are developing an application then set the layout width and height as match parent
and use 9 patch image for more exact value what you want
After Draw9Patching some image I had in my drawable folder, I want to make it have a certain height.
At start I have this picture :
Then, when I apply ScaleType to "fitXY" in order to "strech" my image, and when I define my height, it's incorrect :
The arrow on the bottom of my imageview get stretched, however I mentioned on my draw9patch not to stretch this area.
Do you have any explanation of this problem ?
Thanks in advance!
I'm pretty sure 9patches are meant to be used as backgrounds of Views, not as the source of an ImageView. If you set the background of a TextView to this 9patch, everything will work fine.
I'm new to Android dev, I'm trying sth simple: I want to place a set of 3 buttons horizontally in a line, each with its own background PNG image. So I create a linear layout (horiz) and put three Button elements, each with a weight of 1, and set each button's Background property to some png file. The buttons' width/height is set to wrap_content.
But what I get is that the png does not cover the entire button horizontal area. Instead, the background image is scaled down and keeps some sort of margin from the button view outline as shown in the visual designer. This also shows up in the emu. I tried forcing 0 in the layout's padding/margin - nada.
any idea on how to actually get the background image of a button to cover the button's background??
tnx
I believe you would need to make a new 9-patch, as that small margin is saved into the button image itself.
Try negative values for the buttons layout_margin.