132

I have an imageview that has its height and width set to fill_parent with a linearlayout that has the same values set. So I suppose this should set my images to fit the screen. But it only fits like 80% (margin top and bottom in landscape mode).

I've tried the following code without success:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

imgView.setMinimumWidth(width);
imgView.setMinimumHeight(height);

imgView.setMaxWidth(width);
imgView.setMaxHeight(height);

Any other ideas?

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Johan
  • 2,149
  • 4
  • 21
  • 18
  • Although it's a slightly different problem, [this answer](http://stackoverflow.com/questions/2991110/android-how-to-stretch-an-image-to-the-screen-width-while-maintaining-aspect-rat/2999707#2999707) should help you. – fredley Jan 12 '11 at 11:51

10 Answers10

332

to change pro-grammatically use :

imgview.setScaleType(ScaleType.FIT_XY);

OR

to change from xml use:

 android:scaleType="fitXY"
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Pinki
  • 21,723
  • 16
  • 55
  • 88
  • 27
    I know this is what you wanted, but for images where you care about aspect ratio, this will stretch it out. – Artem Russakovskii Nov 15 '11 at 01:39
  • Works wonders for ImageView inside a ScrollView. Even the aspect is kept correct. – JNissi Oct 17 '12 at 04:52
  • 12
    The same can be defined in the layout xml, android:scaleType="fitXY" – Sathesh Oct 17 '14 at 10:46
  • Now my pictures look fat – DeathRs May 16 '16 at 06:07
  • 13
    To make the image fill all the area and keep a good aspect ratio use `v.setAdjustViewBounds(true); v.setScaleType(ImageView.ScaleType.CENTER_CROP);` – Marco Florian Jun 20 '16 at 09:23
  • 1
    @MarcoFlorian Why not just use FIT_CENTER? Is there any reason? – Jenix Aug 01 '17 at 05:46
  • @Jenix `FIT_CENTER` will fit it horizontally or vertically leaving the rest of the area empty. https://4.bp.blogspot.com/-8SBHoA5e0M4/VeFSX8HX7qI/AAAAAAAAbI0/z2STMO_cAeY/s1600/AndroidImageViewScaleType_Fit_Center.png – Marco Florian Aug 02 '17 at 07:13
  • @MarcoFlorian Yeah, I know what FIT_CENTER and CENTER_CROP are, and that's why I'm using FIT_CENTER most of the time over CENTER_CROP in my apps so far. But I didn't know about the 'v.setAdjustViewBounds(true)' thing so tried with CENTER_CROP as you did, and surprisingly, it worked just like FIT_CENTER. I'm happy to know new thing but asked you the question because if there's any reason writing two lines over one line.. – Jenix Aug 02 '17 at 09:03
  • @Jenix, didn't notice this comment till now. `v.setAdjustViewBounds(true)` will make it fit the area boundaries if the image is bigger or smaller. `FIT_CENTER` or `CENTER_CROP` are applied then. Do your tests with small and big images, you will notice the differences this way. – Marco Florian Nov 29 '17 at 17:52
  • @Marco Florian Thanks for the reply. When I asked the question, I was working with some projects and tested on one of it. I don't know which one it was, and don't remember I actually tested with both bigger and smaller images even though I think I would, and did. So I gave it another try with an empty project, but this confuses me more.. Calling setAdjustViewBounds() with both true and false doesn't affect at all. – Jenix Nov 30 '17 at 10:39
  • Whether it's true or not, setting to ImageView.ScaleType.CENTER_CROP is always CENTER_CROP, and setting to ImageView.ScaleType.FIT_CENTER is always FIT_CENTER. At that time, I asked you purely out of curiosity but I think I need to take some time trying many different cases. Thanks. – Jenix Nov 30 '17 at 10:39
  • 1
    Works like a charm – dave o grady Mar 26 '18 at 20:39
119

Give in the xml file of your layout android:scaleType="fitXY"
P.S : this applies to when the image is set with android:src="..." rather than android:background="..." as backgrounds are set by default to stretch and fit to the View.

Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82
  • the background thing didn't help solve the problem for me, am i doing something wrong? please take a look at: http://stackoverflow.com/questions/14164473/imageview-is-not-stretching-to-fit-the-screen – Dheeraj Bhaskar Jan 07 '13 at 20:49
  • 1
    This doesnt take care of aspect ratio if you hardcode the images' width and want the height to scale – Akshat Agarwal Dec 05 '13 at 21:15
  • How can I get this to fit the whole screen then? I want it to still be proportional – Ruchir Baronia Nov 07 '15 at 21:27
  • +1 for telling it applies to android:src="..." rather than android:background="..." this is very important – DragonFire May 11 '19 at 14:56
22

use this:

ImageView.setAdjustViewBounds(true);
bummi
  • 27,123
  • 14
  • 62
  • 101
Lalit kumar
  • 2,377
  • 1
  • 22
  • 17
  • 4
    This in combination with `android:scaleType="fitXY"` is what worked. Thanks – smac89 Jul 26 '15 at 19:20
  • @smac89 What is the difference between those combination and just using FIT_CENTER? Should not use FIT_CENTER? – Jenix Aug 01 '17 at 05:48
  • @Jenix, mind you it's been a while since I last touched Android, but I believe the reason for doing `fitXY` is so that the image is stretched proportionally to fit the frame/view it was set in. Using `FIT_CENTER` sounds like it will just place the image in the center of the view but does not fill the view. I may be wrong, but you can experiment with either to find out what works – smac89 Aug 01 '17 at 15:19
  • @smac89 Before asking I already tested both of them and it worked just the same. I was curious about the reason but now think you didn't know FIT_CENTER just like the fact I didn't know about the combination you let me know. :) – Jenix Aug 02 '17 at 09:11
9

if you use android:scaleType="fitXY" then you must specify

android:layout_width="75dp" and android:layout_height="75dp"

if use wrap_content it will not stretch to what you need

<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/listItemNoteImage"
android:src="@drawable/MyImage"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:scaleType="fitXY"/>
Rakshith Kumar
  • 872
  • 8
  • 20
Ozear
  • 99
  • 1
  • 1
7

The accepted answer is perfect, however if you want to do it from xml, you can use android:scaleType="fitXY"

Bibaswann Bandyopadhyay
  • 3,389
  • 2
  • 31
  • 30
2

I Have used this android:scaleType="fitXY" code in Xml file.

0

just change your ImageView height and width to wrap_content and use

exampleImage.setScaleType(ImageView.ScaleType.FIT_XY);

in your code.

0

for XML android

android:src="@drawable/foto1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
0

Maybe this is irrelevant. This how I do it on android studio.

Put this in the xml:

android:adjustViewBounds="true"

android:scaleType="centerCrop"

adjustViewBounds = will fit the image according to the ratio

scaleType = crop out all the white empty spaces

Then, set the

layout_width = match_parent 
layout_height = warp_content

will become like this

-11

Trying using :

imageview.setFitToScreen(true);
imageview.setScaleType(ScaleType.FIT_CENTER);

This will fit your imageview to the screen with the correct ratio.

Lincy
  • 1,033
  • 1
  • 8
  • 14