Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView not keeping max height and width

I load an image into an ImageView using .setImageURI(selectedImageUri) that is retrieved from the user's photo gallery. I have the image view restrict the size with

android:maxHeight="150dp" android:minHeight="150dp"  android:maxWidth="150dp"  android:minWidth="150dp" 

It's fine up until the you add the image from the gallery. The image then forgets about the max height and width. It is as wide as the display and there is a lot of space above and below the image. I had to add a scrollview just to see the image and scroll down. there is an even amount of space above and below. Has anyone run into this problem before?

like image 698
bwoogie Avatar asked Feb 12 '12 23:02

bwoogie


People also ask

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.

How do I make an image fit in ImageView?

try adding android:scaleType="fitXY" to your ImageView . This will modify the aspect ratio if the original image is not squared. fitXY will almost always change the aspect ratio of the image.

Can ImageView be clickable?

For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

What is SRC in ImageView?

The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.


2 Answers

Add the following to your XML code (Or use the appropriate function)

android:adjustViewBounds="true" 

I don't know why this isn't a default, because it seems like it would be required for a large number of items. Still, adding this simple logic to your XML file will make all of the difference.

like image 156
PearsonArtPhoto Avatar answered Sep 19 '22 11:09

PearsonArtPhoto


I havent. So, I ll be just guessing

  1. Did you try defining a ScaleType option?
  2. Check for opposing layout attributes that can cause the layout inflater to ignore the desired width/height values.
  3. I suppose you tried this already but use just Layout_height /Layout_width instead of min/max attributes..
like image 35
Orkun Ozen Avatar answered Sep 18 '22 11:09

Orkun Ozen