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.
Related
I want to make something like an analog clock with a background image and many TextViews.
The TextViews will get the data programmatically, which is working. But my problem is the design. How can I set the TextView's position in relation to the background image? It should work on different screen sizes ...
Is there maybe a way to take the middle of the screen and set a margin to the center for each of them, like I foretold with the arrow in the picture?
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 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"
Is there any way of resizing an image in a button? The image source is from the drawables folder. I want to have the following layout for the button.
I know that we can use the android:drawableLeft XML tag for the button, but the image is crossing the boundary of the button and thus getting cut off on all sides.
I want my entire image to be in within "Image here" slot from the above depicted layout. How can I achieve this?
If you are using a default button, use the default padding (don't set the padding values) and the image should not cross the boundary.
Alternatively for finer control you can place an ImageView and TextView within a layout (e.g. LinearLayout) and set the layout's style="#android:style/Widget.Button".
I'm trying to create an EditText that has the following background images (aligned right) when a user inputs valid/invalid text:
How can I achieve this in Android? I've tried setting the background image using:
editText.setBackgroundResource(R.id.icon_tick), but when I do this, the whole image gets stretched to fit the EditText.
editText.setCompoundDrawables(left, top, right, bottom) where left, top, right and bottom are either drawables or null.
Why does it have to be a background image? Just make it a standard image and position it accordingly in your layout. Otherwise, you'll need to make a large image that actually takes up the entire background of your view (with your icons positioned where you want them). You'll also need to size this image accordingly for the various screen resolutions.
An alternative approach can also be found here:
Background Image Placement
Possible duplicate post.
You can make the drawable as nine patch image. Have some transparent area to the left of the tick mark and define it as stretchable area. That should do the trick.