Android ImageView

Posted on 19 Nov 2011 by Eric Oestrich

Android’s ImageView does a strange thing when you have it stretch to fit the width of the view but not the height. It creates a big square for the view instead of conforming to the image size.

This:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/red" />
</RelativeLayout>

Gives you:

Android ImageView no bounds

In order to fix this you can set the property “android:adjustViewBounds” to true, and the view will conform to the size of the image.

This:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/red" 
        android:adjustViewBounds="true"/>
</RelativeLayout>

Gives you:

Android ImageView with bounds

Much better.

Found from: http://stackoverflow.com/questions/5355130/android-imageview-size-not-scaling-with-source-image

comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.