I have an ImageView that scales down when I set the layout width to fill parent. But when I change it to an ImageButton, it doesn't scale. How do I get the ImageButton to look like the ImageView?
Use the android:scaleType attribute (set to center inside or something like that).
However, even if you can get it to scale, I am not sure if you'll get the result you want. The imagebutton displays a regular button, but with an image instead of text, that is you'll get the button-borders around your image. If the imageview works as you want it to, why don't you keep it as an imageview, but set the focusable and clickable attributes on it?
As Jave suggested, set scaleType to centerCrop. And also set padding to 0dp to take out button borders.
Related
I have a background image, which is simply colored panel such as silver purple, etc.,
This background image is set as andorid:background of RelativeLayout, which consist of a ImageView
I want that imageview's image resource to be appear like in center of the background image.
I tried alignParentTop and marginTop to position the combo image in center.
It works as expected, but when trying on different devices, as marginTop is hardcoded such as 10dp,, or 5dp, the alignment looks differently on different devices with different resolution.
I have even created various dimens.xml files for various configuration with various marginTop dp values. Still it only works on specific devices and doing fro all devices is obviously impossible. So i need to set or align the background and foreground images such that foreground image is centered over background image.
Note: Using Framelayout is not of my concern. I want this by RelativeLayout.
On the ImageView and the parent RelativeLayout, remove all of the padding and margin stuff you have, then add android:layout_centerInParent="true" to the ImageView. If you know the height and width of the image in the ImageViewbeforehand, set the ImageView to match it identically (in dp's). Otherwise, try setting the ImageView height and width to wrap_content and potentially android:adjustViewBounds="true".
Things looked quite simple first but in the end the result is not good.
I have an image which has a width larger than screen's width. So I need to scale it down in my imageview. I looked over the ScaleType options and tried them all but none is ok. First "center" only displays the image centered on the layout, no scaling done. "fitCenter" scales the image to fit in my layout but has a major drawback: the height of the item remains as it would have the large image in it. Take a look at the second screen in the attached image. How can I force the list item, to reduce its height to wrap both the text and the image ?
Use the scaletype which seems best to you ( I guess you like what you see with fitCenter). The additional thing that you must do is
android:adjustViewBounds="true"
in your ImageView.
or you could go with FitXY but sometimes the result is not exactly what you want.
use android:scaleType="fitXY"
Could you use FitXY?
This would work if you knew the size of the area you were putting the image into.
CentreInside may also work, I've used this to scale down images, but I think it depends if you've control of the size of the bounding layout element.
You either need to set android:height = "wrap_content" on your outer container, or set static height of TextBox and give ImageView android:weight = "1" so that the ImageView takes remaining space in container.
I have an image view whose background is a xml shape drawable i.e. a rectangle shape drawable.My app's orientation is fixed to landscape view.
The imageview is in relative layout.
I am trying to move it to the right of the screen by setting the appropriate value of layout_marginRight but this does not work .The imageView always stays in its's original position.
I have tried the following other options also but none helped.
The other options which I tried are:
Creating a new relative layout params and setting the right margin
Creating new margin layout params and setting the position
Trying padding option
Setting the imageview to right position relative to another imageview...
Using display metrics to get width of screen and accordingly setting the margin....
I am stuck since a week setting the position of this imageview...
I was thinking the best approach is to set this imageview in between two imageview as I am not able to move it by setting margin but that does not work either...
Here is the current xml of my imageview in main.xml:-
<ImageView
android:id="#+id/rect1"
android:background="#drawable/rect"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="70dp"
android:scaleType="fitCenter"/>
I don't see why adding a margin to the right of the image would help move the image to the right, it just extends the right side bounds of the imageview by the margin. As long as the imageview isn't actually on the right side of the parentview, it would be allowed to grow without changing position. I suggest using layout_alignParentRight="true" on the imageview if you want it on the right of your relativelayout, and then you can use the marginRight to control how far off the right side you want it.
Make the layout_width attribute of the RelativeLayout to fill_parent and see it work :)
My issue was that my parent layout's width was set to wrap_content, which doesn't have a defined width during the editor, I think. Thus, I couldn't use marginRight to get an offset. I had to change the width to a fixed size like match_parent or a constant dp size.
put the image view in a linearlayout, then use android:layout_marginRight="70dp
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
I am creating a dynamic table whose rows contains a imageview and a textview. My problem is this imageview is taking full size of original image but I want to change the size of imageview.I have used setLayoutParams which has no effect.
As an alternative I also used textview instead of imageview and set image as textview's background and used setWidth and setHeight but it has the same problem.
Plz Help Me.
Have you tried the scaleType-Attribute?
Check out the setMaxWidth and setMaxHeight-methods of the ImageView-class:
To set an image to be a maximum of 100
x 100 while preserving the original
aspect ratio, do the following: 1) set
adjustViewBounds to true 2) set
maxWidth and maxHeight to 100 3) set
the height and width layout params to
WRAP_CONTENT.
Link
Assuming your image is a drawable resource, you can use ScaleDrawable instead, and use the xml attributes android:scaleHeight and android:scaleWidth to shrink your image.
OK I found the Solution
Resizing an ImageView within a TableLayout programmatically
Try changing your ImageView's layout_width and layout_height inside the layout file to some constant values measured in sp, for example 100sp. This will make all ImageViews look the same.