android: stretch image in imageview to fit screen

AndroidImageview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

to change pro-grammatically use :

imgview.setScaleType(ScaleType.FIT_XY);

OR

to change from xml use:

 android:scaleType="fitXY"

Solution 2 - Android

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.

Solution 3 - Android

use this:

ImageView.setAdjustViewBounds(true);

Solution 4 - Android

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"/>

Solution 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

just change your ImageView height and width to wrap_content and use

exampleImage.setScaleType(ImageView.ScaleType.FIT_XY);

in your code.

Solution 8 - Android

for XML android

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

Solution 9 - Android

Trying using :

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohanView Question on Stackoverflow
Solution 1 - AndroidPinkiView Answer on Stackoverflow
Solution 2 - AndroidNavaneeth SenView Answer on Stackoverflow
Solution 3 - AndroidLalit kumarView Answer on Stackoverflow
Solution 4 - AndroidOzearView Answer on Stackoverflow
Solution 5 - AndroidBibaswann BandyopadhyayView Answer on Stackoverflow
Solution 6 - Androiduser6011162View Answer on Stackoverflow
Solution 7 - AndroidAnkur YadavView Answer on Stackoverflow
Solution 8 - AndroidJosef JandaView Answer on Stackoverflow
Solution 9 - AndroidLincyView Answer on Stackoverflow