48

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?

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
bwoogie
  • 4,339
  • 12
  • 39
  • 72
  • possible duplicate of [Android imageview not respecting maxWidth?](http://stackoverflow.com/questions/3530328/android-imageview-not-respecting-maxwidth) – AZ_ Jul 07 '15 at 07:37
  • If rest answers are not working for you visit https://stackoverflow.com/a/47317310/6332104 – Satish Rajbhar Nov 15 '17 at 21:17

2 Answers2

165

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.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 2
    @MrAppleBR: You have no idea how long this took me to figure out... Glad to be of service! – PearsonArtPhoto Aug 16 '13 at 21:04
  • 4
    @Aggressor if you need a fixed value you don't need setMaxWidth and setMinWidth, just set android:layout_width="50dp" – marmor Mar 14 '16 at 08:23
  • 3
    @Aggressor You also should avoid using fixed widths like that unless you have a very specific case. The reason that works on iOS is you know what your screen size and resolution is. In Android it is very variable. – zgc7009 Mar 31 '16 at 14:33
  • 2
    @zgc7009 Unless you...dynamically generate the width value from a percent of the screen size ;) – Aggressor Mar 31 '16 at 22:01
2

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..
Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103