Resources, qualifiers and themes

Learn how Android picks resources with qualifiers, how themes and styles combine, and how dark mode, density and language variants load without code. About 22 minutes.

Why it matters

  • A missing or wrong qualified resource does not crash. The app quietly falls back to another folder, which is how dark text ends up on a dark background on some devices only.
  • Interviewers ask you to trace which folder wins when values-fr/, values-sw600dp/ and values-night/ all exist; the answer follows a fixed precedence order that many developers get wrong.
  • Qualifiers are how one APK serves phones, tablets, foldables, dark mode and dozens of languages without if branches in code.
  • Theme attributes (colorPrimary, colorOnSurface) are the contract between your app and Material components; with the wrong parent theme, every component looks wrong.

The simple explanation

  • Think of a restaurant that serves the same dish name but swaps the recipe by region, season, and dietary tag.
  • The kitchen (the build) prepares every variant and labels each jar with its tags.
  • At service time the host (the resource system) throws away jars whose tags do not fit the guest, then looks at the tags in a fixed order of importance and keeps the jars that have the most important matching tag.
  • If no tagged jar fits, the host uses the plain, untagged jar.
  • A theme is the table setting: it decides the plate, cutlery, and napkin colour for every dish at once.

Android stores strings, colours, dimensions, drawables and layouts in res/ folders. A qualifier is a suffix on the folder name that ties the resources inside to a device configuration: values-night/, values-sw600dp/, drawable-xxhdpi/, values-fr/. At build time AAPT2 compiles everything into one resource table where each resource name can have several configuration-specific values. At runtime, Resources picks the value that best matches the current configuration, separately for each resource.

Qualifiers have a fixed precedence. Some common ones, highest first:

PrecedenceQualifier exampleWhat it pins
1values-fr, values-fr-rCAlanguage and region
2values-sw600dpsmallest width
3values-w600dpavailable width
4layout-landorientation
5values-nightnight mode
6drawable-xxhdpiscreen density
7values-v31minimum API level
—values/default, used when no qualified value fits

The selection rule:

  1. Drop every folder whose qualifiers contradict the device (for example values-night on a light-mode device, or values-sw600dp on a phone).
  2. Take the highest-precedence qualifier. If any remaining folder has it, drop the remaining folders that do not.
  3. Repeat for the next qualifier until one value is left.

So for a French phone in dark mode, values-fr/ beats values-night/, because language comes first. Density is the exception: Android does not drop non-matching densities, it picks the closest density and scales the image.

Styles are reusable attribute bundles applied to one view with style="@style/...". A theme is applied to an Application, Activity, or a view subtree with android:theme, and sets attributes that every view in that context can read, such as colorPrimary, colorOnSurface, android:windowBackground. With a DayNight parent theme, Material components already switch between light and dark; you only override the colour values, for example in values-night/themes.xml or with night-qualified colour resources.

Plurals live in strings.xml as <plurals name="items"> with <item quantity="one"> and <item quantity="other"> children. Call resources.getQuantityString(), which picks the form the current language's plural rules require. If the required form and other are both missing, it throws Resources.NotFoundException.

Notes

  • values/ holds the defaults. Always provide a default for every resource, or a device that matches no qualified folder crashes with Resources.NotFoundException.
  • Selection works per resource. If values-fr/ contains only strings, colours still come from values/ or values-night/.
  • Precedence, among the common qualifiers: language/region, smallest width, available width, orientation, night mode, density, API level. Qualifiers in a folder name must appear in that same order (values-fr-sw600dp-night, not values-night-fr).
  • Density is special: Android picks the closest density and scales, instead of dropping non-matching folders. Use drawable-nodpi for images that must never be scaled, and prefer vector drawables.
  • A style is applied to one view. A theme is applied to an Application, an Activity, or a view subtree (android:theme, a theme overlay), and its attributes are read with ?attr/....
  • With a DayNight parent, dark mode needs no Kotlin code: provide night values in values-night/ (colours or the theme). The uiMode configuration change recreates the Activity by default and the night resources are picked.
  • Plurals: English needs one and other; other languages need other forms (few, many, ...). Always include other. Lint's MissingQuantity check reports missing forms for each translation.
  • values-w600dp/ and values-h600dp/ match the current window size, so they also react to multi-window and folding. For layout decisions in code, window size classes are the current recommendation.
  • Resource IDs are generated at build time and can change between builds. Never hard-code an integer such as 0x7f0a0001; always use R.string.foo.
  • App module resources override library resources with the same name. Prefix library resource names (resourcePrefix in the library's build file) to avoid accidental overrides.

Code example

A Material 3 theme in values/, its dark-mode counterpart in values-night/, and a plural string selected at runtime.

<!-- res/values/themes.xml -->
<resources>
  <style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
    <item name="colorPrimary">#6750A4</item>
    <item name="colorOnPrimary">#FFFFFF</item>
    <item name="colorSurface">#FFFBFE</item>
    <item name="colorOnSurface">#1C1B1F</item>
  </style>
</resources>
 
<!-- res/values-night/themes.xml -->
<resources>
  <style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
    <item name="colorPrimary">#D0BCFF</item>
    <item name="colorOnPrimary">#381E72</item>
    <item name="colorSurface">#1C1B1F</item>
    <item name="colorOnSurface">#E6E1E5</item>
  </style>
</resources>
 
<!-- res/values/strings.xml -->
<resources>
  <plurals name="cart_items">
    <item quantity="one">%d item in cart</item>
    <item quantity="other">%d items in cart</item>
  </plurals>
</resources>
  • Both files declare the same style name Theme.MyApp; the system picks the values-night/ copy when the device is in dark mode. No Kotlin code is needed.
  • The parent Theme.Material3.DayNight.NoActionBar already switches Material components between light and dark; here you only override the colour values.
  • Views and layouts should read colours through the theme, for example android:textColor="?attr/colorOnSurface", so they follow the mode automatically.
  • Call resources.getQuantityString(R.plurals.cart_items, count, count): the first count picks the form, the second fills %d.
  • Status bar colours are not set here: when you target API 35+, apps are drawn edge-to-edge on Android 15+ devices and android:statusBarColor has no effect there.

Run the idea in plain Kotlin

A model of the selection rule for one resource that every folder defines. It covers three qualifiers in precedence order: language, smallest width, night mode.

data class Device(val language: String, val smallestWidthDp: Int, val night: Boolean)

data class Folder(
  val name: String,
  val language: String? = null,
  val minSw: Int? = null,
  val night: Boolean? = null,
)

// Step 1: a folder is a candidate only if none of its qualifiers contradict the device.
fun fits(f: Folder, d: Device): Boolean =
  (f.language == null || f.language == d.language) &&
  (f.minSw == null || d.smallestWidthDp >= f.minSw) &&
  (f.night == null || f.night == d.night)

fun resolve(d: Device, folders: List<Folder>): String {
  var c = folders.filter { fits(it, d) }
  // Step 2: walk the qualifiers from highest precedence to lowest.
  if (c.any { it.language != null }) c = c.filter { it.language != null }
  if (c.any { it.minSw != null }) {
      val best = c.mapNotNull { it.minSw }.max()       // closest smallest width wins
      c = c.filter { it.minSw == best }
  }
  if (c.any { it.night != null }) c = c.filter { it.night != null }
  return c.single().name
}

fun main() {
  val folders = listOf(
      Folder("values"),
      Folder("values-night", night = true),
      Folder("values-sw600dp", minSw = 600),
      Folder("values-sw600dp-night", minSw = 600, night = true),
      Folder("values-fr", language = "fr"),
  )

  println("French phone, dark   -> " + resolve(Device("fr", 411, true), folders))
  println("English tablet, dark -> " + resolve(Device("en", 800, true), folders))
  println("English phone, light -> " + resolve(Device("en", 411, false), folders))
}

Expected output: values-fr (language beats night mode), values-sw600dp-night, and values.

In production

Unreadable settings screen on tablets in dark mode
  • After a release, users on tablets reported a settings screen with almost invisible text in dark mode. There was no crash.
  • The tablet layout in layout-sw600dp/ set android:textColor="#212121", a hard-coded dark grey.
  • The phone layout used ?attr/colorOnSurface, so only tablets in dark mode showed dark text on a dark surface.
The fix
  • Reproduced on a tablet-sized emulator with adb shell cmd uimode night yes.
  • Replaced the hard-coded colour with ?attr/colorOnSurface, so the theme value is used in both modes.
  • Added a review rule and a simple CI check that flags hard-coded # colours in layout files, and added screenshot tests for light and dark on phone and tablet sizes.
Plural string crash in one language
  • Users in one locale saw a Resources.NotFoundException from the cart badge.
  • The translated plurals block had quantity="one" and quantity="few" (copied from another language) but no quantity="other".
  • That language's rules asked for other for most counts, and with neither the required form nor other present, getQuantityString threw.
The fix
  • Added the missing <item quantity="other"> to every translated plurals block.
  • Raised lint's MissingQuantity check to an error so an incomplete translation fails the build.
  • Asked translators to work from each language's plural forms, not from the English file's structure.

Common mistakes

  • Hard-coding a colour hex in a layout. It does not change in dark mode, so text becomes invisible or low-contrast. Use a theme attribute such as ?attr/colorOnSurface, or a colour resource with a values-night/ variant.
  • Forgetting quantity="other" in a plurals block. Most languages use other for many counts. Without it, getQuantityString can throw Resources.NotFoundException. Always include other, plus the forms each language needs.
  • Using density qualifiers to target tablets. Density does not mean form factor: a phone can be xxhdpi and a tablet xhdpi. Use sw600dp or w600dp for form-factor splits.
  • Using a different theme name in values-night/themes.xml. A night value only overrides a resource with the same name. A differently named style is just an unused extra style, and the light values stay active. Keep the name identical.
  • Expecting a qualifier lower in precedence to win. A colour defined in values-fr/ beats the same colour in values-night/ on a French device in dark mode. Keep colours out of language folders.
  • Using a non-DayNight parent theme. With Theme.Material3.Light, the components stay light even in dark mode, whatever you put in values-night/. Use a DayNight parent.

Interview questions

Answer out loud first. Then reveal the answer and compare it at each depth.

“How does Android decide which resource value to use at runtime?”

“What is the difference between a Style and a Theme?”

“How do you support dark mode without writing any Kotlin code?”

“A user reports that a string shows '1 items' instead of '1 item'. What do you check?”

Quiz

A device is in dark mode, has xxhdpi density, and uses French. Both values-night-xxhdpi/ and values-fr-night/ define the same colour. Which one is used?

What happens if values-night/themes.xml declares a theme with a different name from the one in values/themes.xml?

Which call returns the correctly formatted plural text for a count of 3?

What does the -sw600dp qualifier match?

0 of 4 answered

Mini task

Add dark-mode support and a plural string to your project

  1. Open res/values/themes.xml. Note the name of your app theme (for example Theme.MyApp) and check that its parent is a DayNight theme.
  2. Create res/values-night/themes.xml. Use the same theme name and parent, and change colorPrimary, colorOnPrimary, colorSurface, and colorOnSurface to dark values (for example #D0BCFF, #381E72, #1C1B1F, #E6E1E5).
  3. In res/values/strings.xml, add a <plurals name="photo_count"> block with quantity="one" set to %d photo and quantity="other" set to %d photos.
  4. In your main Activity or Compose screen, call resources.getQuantityString(R.plurals.photo_count, 3, 3) (or pluralStringResource in Compose) and log the result.
  5. Switch dark mode with the Quick Settings tile or adb shell cmd uimode night yes, and confirm the colours change while the app keeps running.
  6. Add res/values-fr/colors.xml with one colour also defined in values-night/, switch the device to French and dark mode, and confirm the French value wins.

Done when: the surface and primary colours flip between light and dark when you toggle the setting, Logcat shows 3 photos, and you can explain why the French colour won in step 6.

Next: Logcat and the debugger