question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageView with one of its dimensions set to wrap_content does not work

See original GitHub issue

Describe the bug Assume the following ImageView:

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

If you try to load an image to it, Coil will not calculate the correct width (based on the ImageView’s fixed height and the aspect ratio of the image being loaded), and the ImageView will end up with a 0 width.

Using size() solves this issue, but it is not ideal because it is not always possible to know beforehand the dimensions of the image being loaded.

To Reproduce Load an image to an ImageView which has one fixed dimension, and one wrap_content dimension. Sample project: CoilWrapContent.zip

Expected behavior Coil should automatically calculate the dimension set to wrap_content based on the fixed dimension & the dimensions of the image being loaded.

For example: If I load a 400x200 (2:1 aspect ratio) image to an ImageView with android:layout_width="wrap_content" and android:layout_height="100dp", then Coil should automatically set the width to 200dp (perhaps this shouldn’t apply to all scaleTypes).

Logs/Screenshots Layout Inspector screenshot showing the ImageView having its width set to 0: image

Library version 0.6.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
colinrtwhitecommented, Aug 23, 2019

@fernandospr You can work around this by setting the scaleType to centerCrop or scale(Scale.FILL) on your request.

This occurs because wrap_content is evaluated to 1px whereas Glide and Picasso treat it as the height of the parent. I’m working on fixing this behaviour for 0.7.0.

3reactions
colinrtwhitecommented, Aug 13, 2019

Good catch! I think this is because we force PixelSize’s dimensions to be > 0. Relaxing that limitation to >= 0 should solve the issue, but might cause side-effects. Going to work on this later tonight.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Android imageView wrap_content does not work
When I set an image with a size of 1080x500, the ImageView takes up the entire screen in width, but for some reason...
Read more >
Working with the ImageView | CodePath Android Cliffnotes
Fixing the width and height however means that the proportions of the width and height of the original image, known as the aspect...
Read more >
Build a Responsive UI with ConstraintLayout
When you add a constraint to both sides of a view (and the view size for the same dimension is either "fixed" or...
Read more >
A guide for Android ImageView ScaleType and ...
Fortunately there is a really simple way to preserve the aspect ratio of the image while keeping control of one dimension (in our...
Read more >
How to Remove an Image from ImageView in Android?
So if the width and height of the ImageView are set to wrap content, then the ImageView shall occupy the area on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found