15
votes

If I define drawables for a density qualified folder (eg drawable-hdpi), and also drawables to fall back on in drawable-nodpi, will a high density device use the -hdpi over the -nodpi?

What about if I take it a step further and also have the same setup for -land folders.

4
For any future viewers of this question: if you do want a qualified -land folder that also includes -density, make sure to put -land first. See this table.theblang

4 Answers

58
votes

I'm not sure what the precedence is for nodpi, but that should never be a problem. It sounds like you are misunderstanding the nodpi qualifier. You should not use nodpi as a fallback for assets that you don't provide at the device's density bucket. The correct fallback is a folder with no density qualifier (e.g. drawable/).

If the system cannot find an asset at the device's density (e.g. it is an ldpi device and you don't have a drawable-ldpi folder), it will fall back to a folder without a density qualifier, *not the nodpi qualifier`.

The nodpi qualifier is used when you want to specify a resource that will be used for all densities and that you do not want Android to scale. Assets in the other density folders (e.g. drawable-xhdpi) will be scaled to the actual screen size. If you use the nodpi qualifier, you should not provide that asset in any other resource folders.

It is also important to note that with screen density qualifiers, Android will also prefer to use a lower density asset over an unqualified resource. If you have an xhdpi device, but you only have a drawable and a drawable-mdpi folder, Android will check for the asset in the mdpi folder before the unqualified folder.

10
votes

drawable-nodpi will bypass scaling and drawable will use the default scaling:

  • mdpi = 1x
  • hdpi = 1.5x
  • xhdpi = 2x
  • xxhdpi = 3x
  • xxxhdpi = 4x

    drawable-nodpi is efficient if your code will be doing its own scaling (or no scaling) and you don't want the image pre-scaled by Android.

    There is also drawable-anydpi, just to make things more confusing.

    drawable with no specifications will be used if an exact match on density and screen specifications does not exist. drawable-nodpi will be used after drawable.

    UPDATE If you have both drawable and drawble-nodpi, the select order is either a more complex rule not documented or Android is broken. Through experimentation I confirmed that devices with screen density < xhdpi will correctly select the drawable image. Devices with screen density >= xhdpi will select the drawable-nodpi.

    Selection rule: 1. Pick match to screen density, one of these:

    • drawable-ldpi
    • drawable-mdpi
    • drawable-hdpi
    • drawable-xhdpi
    • drawable-xxhdpi
    • drawable-xxxhdpi
    1. If no match on density, then select one of these
    • drawable (automatic scaling mdpi=none... xxxhdpi=4x)
    • drawable-nodpi (no scaling)
    • drawable-tvdpi
    • drawable-anydpi (no scaling)
  • 9
    votes

    It depends.

    First of all nodpi is not a fallback folder. If you have a hdpi device, the system will look for hdpi folder first. nodpi folder contains resources that are not meant to be scaled.

    drawable/ can be used as a fallback folder in case device density specific resources are not present.


    Then, if we look at the possible Qualifier Values for the Screen Pixel Density(dpi), these are listed as:

    1. ldpi
    2. mdpi
    3. hdpi
    4. xhdpi
    5. xxhdpi
    6. xxxhdpi
    7. nodpi (Non-scaling resources go here)
    8. tvdpi
    9. anydpi (Resources in this folder take highest precedence)
    10. nnndpi

    Note: You should put all those resources in the drawable-nodpi folder that you do not wish to be scaled. To support multiple screens, Android prefers to scale down a larger original image instead of scaling up a smaller original image. These resources should not be present in any other drawable-qualifier folder else these might be scaled which kind of defeats the whole purpose.


    It must also be noted that:

    Using a density qualifier does not imply that the resources are only for screens of that density. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.


    Here is the Resource Selection Flowchart that the system uses to find the best match:

    enter image description here

    3
    votes

    The drawable-nodpi qualifier is used when image/asset is not need to be scaled.

    Drawable in the other density folders (e.g. drawable-xhdpi) will be scaled to the actual screen size.

    drawable-nodpi for constant size in all density devices.