Viktor Brešan Viktor Brešan - 27 days ago 15
Android Question

How to expand ImageView's image vertically and wrap horizontally?

I am trying to show an image in a way that it fills available screen height and that is 'wrapped' horizontally. Preferably, I am looking for xml layout solution.

At the moment I am using the following code:

<ImageView
android:id="@+id/imageViewPDFPreview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@android:color/white"
/>


On Android 5.0.1 (API 21) I get the following result (image is on the right hand side):
enter image description here

You can notice that the image is not expanded vertically but it is wrapped horizontally.

The same code on Android 6.0 (API 23) produces the following:
enter image description here

In this case image is expanded vertically but for some reason it has horizontal padding.

How can I get the behavior that would be the same on both platforms? Preferably in XML only.

Thanks in advance.

Answer

Using android:adjustViewBounds="true" in ImageView and removing android:layout_weight from the parent layout element did the trick.

Comments