Set GridView columnWidth to wrap_content in xml - android

I have a GridView containing ImageView all of the same size.
Each ImageView has its width set to wrap_content, so that I can support multiple screen resolutions by picking the correct version of my Drawable resource.
I can't find a way to automatically set the column size of my GridView to the ImageView's width.
This can probably be done programmatically using
mGridView.setColumnsWidth(mImageView.getWidth());
but I can't find a way to do it using only XML. Does such way exists?

Try this:
android:stretchMode="columnWidth"

Try this-
android:columnWidth="50dp"
Here you can give the dimension.

Related

Android ImageView ScaleType and item height

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.

Setting programmatically the pixel size of a remote ImageVIew?

Let's say that I have a layout that contains
<ImageView
android:id="#+id/my_id"
android:layout_width="123px"
android:layout_height="345px"
android:padding="0dip"
android:scaleType="fitXY" />
which is used in an AppWidget and is set via a RemoteViews. How can I programmatically set the width and height in pixels (overriding the 123 and 345 in the above xml)?
(motivation: this image view shows a dynamic png file that is set at runtime via a file URL. If the layout_width and layout_height above are set to wrap_content, the image is displayed scaled down the the device's density(). Setting the exact width and height in pixels solves the problem but the file size changes and I don't know how to set it programmatically).
You can do it in a different way. Put your ImageView in a LinerLayout and set layout_width and layout_height of ImageView to "fill_parent". Then change LinearLayout's width and height.
I assume you know about app widgets size rules.

Proportionately re-sizing ImageView when width is set to fill parent

Is there a way to proportionally set the height of an ImageView if the width is set to android:layout_width="fill_parent"?
It appears there is no direct way of adjusting an ImageView without skewing one of it's dimensions. This seems bizarre given the application is expected to support a slew of screen sizes and resolutions.
You can use wrap_content for the height and set the correct scale type (either in code or via the android:scaleType attribute in XML). You are probably looking for CENTER_INSIDE/android:scaleType="centerInside".
See ImageView.ScaleType

Image at the specific screen position

Can I using xml layout attributes put image to the following position? Or I must calculate position on src?
http://i.stack.imgur.com/gox4d.png
You can do it using the XML attributes.
Just use follwing attribute in your ImageView:
Android:layout_marginTop="20dip"
Android:layout_height="20dip"
Only use DIP as these are density independent Pixels
dont use PX.
Yes, you can, for the simple example you posted I would use a vertical LinearLayout, with a FrameLayouts to 'fill' the blank space. Set the LinearLayout weightSum to 1, then set the first FrameLayout weight attribute to 0.2 (1/5), and imageview to 0.2. Also set both the frame layout and imageview layout_height values to 0px.
While this solution works, I'm sure there is a better and cleaner way out there. Hopefully someone will post it.
You can use AbsoluteLayout, calculate exact coordinates based on screen size and orientation and then position the image with absolute coordinates.
You can just use weights for different containers in a vertical linear layout.

How to Change size of ImageView Dynamically?

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.

Categories

Resources