Search code examples
androidandroid-layoutandroid-linearlayoutpixel-density

Does `wrap_content` works the same way as Density Pixels?


I have read Android guidelines regarding different screen sizes, but still I have some considerations.

Client has given me an image from PSD file which has certain resolution that fits 1080 X 1920. I just use wrap_content, and it perfectly fits the part of screen.

I am not using DP to define its width-height, If i was using DP it would have adjusted image according to screen sizes.

My questions are,

  1. Does wrap_content works the same way as Density Pixels?
  2. Is it also responsive, and changes the image width-height according to different screens?
  3. If not, then Is it necessary to use DP to support different screen sizes ?

Thanks


Solution

  • The setting wrap_content tells your view to size itself to the dimensions required by its content. In the case of your test, your image is 1080x1920 and your device's screen resolution is likely 1080x1920 as well, hence the perfect fit. Since you set the width and height to wrap_content, Android is simply trying to use as much screen space as it needs to correctly display the amount of content it was supplied. In this case, since the available screen space matches the size of the content, it just fits perfectly.

    But what if the device screen isn't 1080x1920? In that case, Android will only use as much space as it can, but still attempt to fit the image inside the bounds of the available screen space. In other words, the system will appropriately scale the image down to get it in the container you have provided for it. But this can lead to awkward fits if the aspect ratio isn't the same as the image. For instance, see this screenshot below:

    enter image description here

    This image is 1920x1080, but notice that it doesn't quite fit. That's because this nexus 7 screen is 1824x1200 when viewed in landscape. Additionally, the toolbar at the top of the screen is eating up available screenspace, making my viewable area even smaller and more awkwardly shaped. So while the system would love this image to extend all the way to the left and right borders, it can't, because then that would mean the height would be bigger than the viewable space. Since I used wrap_content to display this image, the system is using as much vertical space as it can, and the result is that the image doesn't quite fit the horizontal space.

    So to more directly address your questions, yes wrap_content is a relative size setting that will make it easier to get a consistent look across multiple screen sizes, similar to using dp. But realize that there are hundreds, if not thousands of available Android devices on the market, and they all have varying screen sizes and densities. So your drawables may not always appear the way you want them on every device.

    The way to overcome this is to supply multiple versions of your assets and provide alternate layout files for different screen sizes and densities. Once you do that, all you can do is test, test, and test some more. Use emulators for weird screen densities or devices you don't own, just to make sure you're getting the look you want. In the case of your 1920x1080 image, it looks great on that one device, but how will it fit a large tablet or a tiny handset that is smaller than the resolution of the image? These are situations you must account for in your design.

    I suggest you read these resources, as they are hugely helpful in learning how to deal with issues resulting from varying screen sizes and densities:

    http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/training/multiscreen/screensizes.html