Search code examples
androidandroid-layoutpixel-density

How should you (not) use density independent pixels on Android?


One poster suggested using a project called SDP, as an answer to a question regarding the Android density independence mechanism. He justified it, saying:

It can help Android developers with supporting multiple screens

Why is that a bad answer or a bad idea in general?


Solution

  • Multiple reasons:

    Bad practice

    The approach taken by this project is arguably useless, even destructive.

    Destructive, because it breaks Android density independence. It takes images that need little scaling to match the display's actual pixels per inch property, because they were designed for that device's generalized density. And it scales them up, even up to 2.6 times on 10″ tablets. This must result in blurry or pixelated bitmaps.

    Useless because:

    You don't want bigger physical size on bigger devices in the first place anyway. You don't want to take an app and just scale everything on bigger screens. This is what Apple did when the iPad first came out in 2010 and people hated it.

    What you do want is to put a limit on the width of text, buttons and other UI elements that shouldn't be stretched too much. You also want wider margins. But you handle this, by providing an alternative layout for large screens, not by fiddling with how Android handles the screen density. The documentation agrees with me on that.

    And if you really wanted some graphic to be bigger on a large screen (which you generally shouldn't, the second image here is perfectly fine, except for the horizontal stretching), then you should handle it by providing drawables for every size/density pair that you want to support. For example drawable-hdpi, drawable-xhdpi, drawable-sw720dp-hdpi, drawable-sw720dp-xhdpi. This way the bitmap won't need scaling for those bigger screens and will be displayed in high quality.

    Misleading

    The screenshots do not use the same scale for each device. Nexus 7 looks just as tiny as Nexus One.

    When the scale is preserved, the comparison looks like this when using the project:

    The UI is physically bigger on Nexus 7 because it is scaled up.

    And like this, when not using it:

    The UI is physically the same size on both devices, because Android handles density independence and is not obstructed.

    Irrelevant

    That project deals with physical size and not screen density. What it does is scale units depending on varying physical screen size.

    It does not apply to the question that was asked. Which could be paraphrased as "How does Android generalize an actual screen density?".