Viktor Brešan Viktor Brešan - 1 year ago 125
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:


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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download