imageview height as the wrap_content of the shown image

Scott Johansen picture Scott Johansen · Oct 29, 2013 · Viewed 24k times · Source

I want the width of an ImageView to be set by the parent and the height should be aspect proportional. The reasons for this is that next is shown a TextView that I want to place just under the ImageView.

I can get image to show correctly using

android:layout_width="fill_parent"
android:layout_height="fill_parent"

however the ImageView height become the parent height which is much larger than that of the shown stretched image. One idea was to make parent smaller vertically, but.. there I don't yet know the stretched image size.

The following doesn't work because a small image is not filled up horizontally.

android:layout_width="fill_parent"
android:layout_height="wrap_content"

Messing around with

android:layout_height="wrap_content"

for the RelativeLayout surrounding it all does not help. Also tried FrameLayout and LinearLayout and failed.

Any ideas?

Answer

Kit picture Kit · Jun 3, 2014

You have to set adjustViewBounds to true.

imageView.setAdjustViewBounds(true);