Maximum width and height for ImageView in Android

AndroidResizeImageviewAndroid ImageviewStretch

Android Problem Overview


So I have an ImageView set with

android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"

This image view is used to show a picture that is obtained from the gallery or camera. In both cases, the image is not resized to fit inside the imageview, it just stretches its space as much as it needs.

Any idea how to make it stay inside those bounds?

<?xml version="1.0" encoding="utf-8"?>

http://schemas.android.com/apk/res/android"> http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">

<EditText
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/txtDescription"
    android:layout_below="@+id/txtSubject"
    android:inputType="textMultiLine"
    android:height="80px"
    android:hint="@string/description"></EditText>

<EditText
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtDescription"
    android:layout_width="fill_parent"
    android:id="@+id/txtMorada"
    android:hint="@string/address" />

<ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@+id/txtMorada"
    android:id="@+id/btGPS"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_menu_compass"></ImageButton>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@+id/btGPS"
    android:layout_marginTop="25px"
    android:id="@+id/imgPoint"
    android:src="@drawable/google_logo_small"
    android:maxHeight="100px"
    android:maxWidth="250px"
    android:minHeight="100px"
    android:minWidth="250px"
    android:scaleType="centerInside"></ImageView>

<ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@+id/imgPoint"
    android:id="@+id/btGallery"
    android:layout_below="@+id/btCamera"
    android:src="@drawable/ic_menu_gallery"
    android:layout_alignParentRight="true"></ImageButton>

<Button
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginTop="10px"
    android:id="@+id/btSubmit"
    android:layout_below="@+id/btGallery"
    android:text="@string/submit"></Button>

<ImageButton
    android:layout_height="wrap_content"
    android:id="@+id/btMap"
    android:layout_below="@+id/txtMorada"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_menu_mapmode"></ImageButton>

<TextView
    android:layout_below="@+id/txtMorada"
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@+id/btMap"
    android:layout_height="wrap_content"
    android:id="@+id/lblNewPointLatitude"
    android:text="Latitude"></TextView>

<TextView
    android:layout_width="wrap_content"
    android:layout_toLeftOf="@+id/btMap"
    android:layout_height="wrap_content"
    android:id="@+id/lblNewPointLongitude"
    android:layout_below="@+id/lblNewPointLatitude"
    android:text="Longitude"></TextView>

<ImageButton
    android:layout_below="@+id/btMap"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@+is/imgPoint"
    android:id="@+id/btCamera"
    android:layout_marginTop="25px"
    android:src="@drawable/ic_menu_camera"
    android:layout_alignParentRight="true"></ImageButton>

<Spinner
    android:layout_height="wrap_content"
    android:id="@+id/spCategoria"
    android:layout_width="fill_parent"
    android:prompt="@string/spCategoriaPrompt"></Spinner>

<Spinner
    android:layout_height="wrap_content"
    android:id="@+id/spSubcategoria"
    android:layout_below="@+id/spCategoria"
    android:layout_width="fill_parent"
    android:prompt="@string/spSubcategoriaPrompt"></Spinner>

<EditText
    android:layout_height="wrap_content"
    android:layout_below="@+id/spSubcategoria"
    android:layout_width="fill_parent"
    android:id="@+id/txtSubject"
    android:hint="@string/subject"></EditText>

  </RelativeLayout>
</ScrollView>

Android Solutions


Solution 1 - Android

Try the adjustViewBounds attribute:

android:adjustViewBounds="true"

Solution 2 - Android

have you tried android:scaleType="fitCenter"? Also the dimensions of the view should not be set to wrap_content as pointed out by Mayra

Solution 3 - Android

you have both layout_width and layout_height set to wrap_content, in addition to setting explicit values for width and height. Instead, you should just set layout_width and layout_height to some number value. Also, use dp instead of px. See supporting multiple screen sizes.

Solution 4 - Android

Try including this: xmlns:android="http://schemas.android.com/apk/res/android" Example :

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="400dp"
    android:adjustViewBounds="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:src="@drawable/background_img"
    />

Solution 5 - Android

While all the answers already mentioned, it is required to add

android:adjustViewBounds="true"

it should be also noted that scaling does not work if android:background is used to set the image. To allow scaling make use of src or srcCompat like

<!-- Will not work -->
android:background="@drawable/my_image"

<!-- Works -->
android:src="@drawable/my_image"
<!-- Works -->
app:srcCompat="@drawable/my_image"

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
QuestionMiguel RibeiroView Question on Stackoverflow
Solution 1 - AndroidPedro LoureiroView Answer on Stackoverflow
Solution 2 - AndroidAsahiView Answer on Stackoverflow
Solution 3 - AndroidCheryl SimonView Answer on Stackoverflow
Solution 4 - AndroidSatish RajbharView Answer on Stackoverflow
Solution 5 - AndroidFruchtzwergView Answer on Stackoverflow