19

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.

theblang
  • 10,215
  • 9
  • 69
  • 120
  • 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](http://developer.android.com/guide/topics/resources/providing-resources.html#table2). – theblang Jul 11 '14 at 15:24

4 Answers4

64

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • 1
    I don't understand very well the difference between drawable-nodpi and drawable. In my case I have a background image in a imageView that is matching parents (full screen). Either in drawable or drawable-nodpi the image covers the full screen. So, is in drawable-nodpi and IS scaling. What's the point?? – Sotti Oct 07 '14 at 12:52
  • 2
    ImageView is a bit different- it will take a source image of any form and handle the scaling itself. If you set your drawable as the background of any View, you should observe the correct behavior. I also would never put an image in the `drawable` folder- they should always be in a qualified folder. – Bryan Herbst Oct 07 '14 at 13:39
  • You mean all the images should go in a qualified folder? Why? If you have a background image that fills the screen, would you produce one image for each qualified folder? – Sotti Oct 07 '14 at 14:08
  • 1
    Generally you want to be in control of the scaling, so you (or your designers) produce an image that looks good for each density bucket instead of letting the system scale it. If the background image is just a solid color with a logo or something in the middle, then you should use a combination of a background color and smaller centered logo instead of a single full screen image. `drawable-nodpi` is still a qualified folder, I would just stay away from using `drawable` for image assets (use it for XML assets). – Bryan Herbst Oct 07 '14 at 14:13
  • 4
    Any files put in `drawable` will be handeled by the system in the same way as those `drawable-mdpi` when they're loaded. So it might have lower precedence but the result is the same. My advice would be to never use `drawable` and just put things in `drawable-mdpi` if you were planning to use `drawable`. – thoutbeckers Jul 11 '16 at 14:38
15

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)
  • LanDenLabs
    • 1,566
    • 16
    • 10
    • 4
      drawable does actually scale images (I switched to nodpi folder and I've seen 50% improvement on memory monitor on xhdpi device) – cyborg86pl Nov 17 '16 at 13:41
    10

    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

    Prince
    • 20,353
    • 6
    • 39
    • 59
    • 1
      That is for Qualifier name rules, not the precedence!!! And in the row of screen pixel density, google just sort them by the screen density from low to high, not the precedence of retrieving drawables!!! I'm pretty sure that if you are using an hdpi device, it will take hdpi as the highest precedence, not ldpi, the thing you said does not make sense. – Chandler May 10 '18 at 21:31
    • @Chandler You seem confused. I am quoting directly from Android documentation which is crystal clear. Please elaborate what you don't understand. – Prince Jun 07 '18 at 09:59
    • 1
      No, you are the one seems confused. If it is an hdpi device, then drawable-hdpi folder takes highest precedence. There is no point to take drawable-ldpi as highest precedence. – Chandler Jun 07 '18 at 20:56
    • @Chandler In case of hdpi device, if you do not provide drawable-hdpi resource, the system may use whichever resources are the best match. As I have quoted earlier '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.' – Prince Jun 08 '18 at 06:51
    • Based on that, ldpi (Highest Precedence). and tvdpi (Lowest Precedence) are definitely incorrect. – Chandler Jun 08 '18 at 08:01
    • @ToolmakerSteve I have removed the confusing part and updated my answer. – Prince Jan 31 '19 at 09:49
    3

    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.

    R_K
    • 803
    • 1
    • 7
    • 18
    • Re: "drawable-nodpi for constant size ...". Not necessarily. One possible use of drawable-nodpi is when your layout or your code specifies the target rectangle and scaling method, and you decide not to bother providing multiple density-specific images. (But test page load performance on slower phones; if any noticeable lag, then better to provide the density-specific images.) – ToolmakerSteve Jan 30 '19 at 22:59