skip to content

How does Android choose among res/ directories such as layout-sw600dp, values-night and values-ar when a news app starts on a device?

level: middleimportance: must knowfreq 66%

answer

  1. always ship unqualified defaults
  2. drop what contradicts the device
  3. precedence beats number of matches
  4. smallest width ignores rotation
  5. density is never eliminated

basics

~20 s

Android resolves each resource separately: it discards directories that contradict the device configuration, then walks the qualifier precedence table, keeping only directories with the highest-precedence qualifier any candidate uses, until one remains. Density picks the closest bucket.

solid answer

~40 s

Each resource is looked up on its own. Android first eliminates directories whose qualifiers contradict the current configuration, such as `values-fr` on an Arabic device. Then it walks the qualifier table in **precedence order** (MCC first, then language, and later the size qualifiers, orientation, UI mode, night mode and density, among others): at the first qualifier any remaining directory uses, it keeps only the directories that carry it, and repeats until one is left. Precedence wins over the number of matching qualifiers. Density is the exception: it is never eliminated, and Android picks the closest bucket, preferring to scale down. `sw600dp` tests the shorter side of the window, `w600dp` the current width, so a landscape phone gets `w600dp` but not `sw600dp`. Always ship unqualified defaults, or a missing match crashes.

code

plaintext · 8 lines
plaintext
res/
  layout/activity_article.xml              # phones, either orientation
  layout-sw600dp/activity_article.xml      # tablet-sized windows
  layout-sw600dp-land/activity_article.xml # tablet-sized windows in landscape
  values/strings.xml                       # default strings (required)
  values/colors.xml                        # light colours
  values-night/colors.xml                  # dark-theme colours
  values-ar/strings.xml                    # Arabic strings

go deeper

for a junior

Recall the common qualifiers (sw600dp, land, night, a language code, density buckets) and that unqualified defaults must always exist.

for a middle

Walk through elimination and precedence, explain why precedence beats the number of matches, and contrast smallest width with available width.

for a senior

Diagnose wrong-resource bugs from the configuration, design directories that avoid duplicate layouts, and plan for Android 17 ignoring orientation locks on large screens.

for a principal

Set a resource policy: width-driven layouts over per-device copies, start and end over left and right, and a check that every qualified resource has a default.

## Qualified directories Android keeps every resource under `res/` in a directory named after its type (`layout`, `values`, `drawable`, `mipmap`). A **configuration qualifier** appended with a dash says which devices an alternative is for. A news app that must look right on a small phone, a tablet in landscape, in dark mode and in Arabic might ship: | Directory | Chosen when | |---|---| | `layout/` | the default, for anything no other directory matches | | `layout-sw600dp/` | the window's shorter side is at least 600dp (a tablet) | | `layout-sw600dp-land/` | a tablet that is currently in landscape | | `values/` | default strings, colours and dimensions | | `values-night/` | colours for dark theme | | `values-ar/` | Arabic strings | Naming rules matter. Qualifiers must appear **in the order of the precedence table** (`drawable-night-hdpi/`, not `drawable-hdpi-night/`), only one value per qualifier type is allowed, names are case-insensitive, and qualified directories cannot be nested. Since Android 7.0 (API 24) a locale can also be written as a BCP 47 tag with the `b+` prefix, for example `values-b+es+419`. ## How the best match is found Selection runs **per resource**, every time one is requested (once the configuration is known, the system can skip directories that can never match): 1. **Eliminate contradictions.** A directory whose qualifier conflicts with the device is dropped: `values-fr/` on an Arabic device, `values-night/` in light mode. Density is the one qualifier that is never dropped here. 2. **Take the next qualifier in precedence order**, starting with MCC. 3. **If no remaining directory uses it, move on**; if some do, **eliminate every directory that lacks it**. 4. **Repeat** until one directory remains. When the deciding qualifier is density, Android picks the bucket closest to the device, preferring to scale a larger image down rather than a smaller one up. Two consequences surprise people: - **Precedence beats count.** On an English device in dark mode, `drawable-en/` beats `drawable-night-notouch-12key/` even though the second matches three qualifiers, because language outranks night mode. - **Resources resolve independently.** In Arabic and dark mode, a string can come from `values-ar/` while a colour comes from `values-night/`; you do not need a `values-ar-night/` directory. Walk the news app's `activity_article` layout through a 10-inch tablet held in landscape, set to Arabic, in dark mode: 1. `layout/`, `layout-sw600dp/` and `layout-sw600dp-land/` all survive elimination, since none contradicts the device. 2. Language is unused by any candidate, so it is skipped. 3. Smallest width is used by two candidates; `layout/` is eliminated. 4. Orientation is used by `layout-sw600dp-land/` only, and the device is in landscape, so it wins. The same tablet in portrait would contradict `land`, which removes that directory in step 1 and leaves `layout-sw600dp/`. ## Size qualifiers: smallest width, available width, orientation - **`sw600dp`** tests the smaller of the window's two sides, regardless of rotation, so a typical phone, whose shorter side is well under 600dp, matches it in neither orientation. It suits "is this a tablet-sized window". - **`w600dp`** tests the width available right now, so a phone in landscape at 800dp wide matches it. It suits "is there room for two panes". - **`land` and `port`** go after the size qualifiers in the name. These sizes are the **space available to the app's window** after system bars, not the panel's physical size. In multi-window mode the app sees only its window, and resizing that window triggers a configuration change so the matching runs again. ## Defaults, locales and right-to-left text Always keep **unqualified defaults**. If strings exist only in `values-en/` and `values-ar/`, a device set to German finds no match and the lookup throws, crashing the app; with a default `values/` it shows readable, if untranslated, text. For Arabic, translations live in `values-ar/`, but layouts should not be duplicated into `layout-ar/`. Declare `android:supportsRtl="true"` on `<application>` and write `start` and `end` attributes instead of `left` and `right`; the same layout then mirrors for right-to-left locales. ## Android 17 and large screens Android 16 began ignoring `screenOrientation`, `resizeableActivity`, `minAspectRatio` and `maxAspectRatio` on large screens (smallest width 600dp and up) for apps targeting API 36, with a temporary opt-out. For apps targeting **Android 17 (API 37)** that opt-out is gone. A news app can no longer lock a tablet to portrait, so its `sw600dp` layouts must also work in landscape and in resized windows. ## Pitfalls - Expecting `layout-sw600dp/` on a landscape phone; use `w600dp` for width-driven panes. - Writing qualifiers out of the precedence order the directory name must follow. - Copying whole layouts per locale instead of using start and end. - Shipping qualified resources with no default.

  • Why does a phone rotated to an 800dp-wide landscape window still not get layout-sw600dp?
    Smallest width is the shorter side of the window, and it does not change with rotation; that phone's shorter side is around 360dp. To switch layouts on the current width, use `layout-w600dp`, which matches whenever the available width is at least 600dp.
  • What happens if strings exist only in values-en and values-ar, and the device is set to German?
    No directory matches and there is no default, so the lookup throws `Resources.NotFoundException` and the app crashes. A default `values/strings.xml` turns that into readable fallback text.
  • When does the matching run again while the app is open?
    On a configuration change: rotation, a switch to dark theme, a new locale, or a resized multi-window window. By default the activity is recreated and its views inflate against the new configuration, so a different directory can win.

  • The directory that matches the most qualifiers always wins.
  • layout-sw600dp applies whenever the screen is currently at least 600dp wide.
  • Qualifiers can be written in any order, such as drawable-hdpi-night.
  • An hdpi phone ignores drawables that are not in drawable-hdpi.
  • Strings can live only in locale folders because English is the automatic fallback.
  • Locking screenOrientation keeps a tablet on the phone layout when targeting API 37.

Assumes Android 17 (API 37): apps targeting 37 can no longer opt out of the large-screen (sw600dp and up) override of orientation, resizability and aspect-ratio limits that Android 16 introduced for apps targeting 36.