Resources, qualifiers and themes
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/andvalues-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
ifbranches 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:
| Precedence | Qualifier example | What it pins |
|---|---|---|
| 1 | values-fr, values-fr-rCA | language and region |
| 2 | values-sw600dp | smallest width |
| 3 | values-w600dp | available width |
| 4 | layout-land | orientation |
| 5 | values-night | night mode |
| 6 | drawable-xxhdpi | screen density |
| 7 | values-v31 | minimum API level |
| — | values/ | default, used when no qualified value fits |
The selection rule:
- Drop every folder whose qualifiers contradict the device (for example
values-nighton a light-mode device, orvalues-sw600dpon a phone). - Take the highest-precedence qualifier. If any remaining folder has it, drop the remaining folders that do not.
- 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 withResources.NotFoundException.- Selection works per resource. If
values-fr/contains only strings, colours still come fromvalues/orvalues-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, notvalues-night-fr). - Density is special: Android picks the closest density and scales, instead of dropping non-matching folders. Use
drawable-nodpifor images that must never be scaled, and prefer vector drawables. - A style is applied to one view. A theme is applied to an
Application, anActivity, or a view subtree (android:theme, a theme overlay), and its attributes are read with?attr/.... - With a
DayNightparent, dark mode needs no Kotlin code: provide night values invalues-night/(colours or the theme). TheuiModeconfiguration change recreates the Activity by default and the night resources are picked. - Plurals: English needs
oneandother; other languages need other forms (few,many, ...). Always includeother. Lint'sMissingQuantitycheck reports missing forms for each translation. values-w600dp/andvalues-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 useR.string.foo. - App module resources override library resources with the same name. Prefix library resource names (
resourcePrefixin 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 thevalues-night/copy when the device is in dark mode. No Kotlin code is needed. - The parent
Theme.Material3.DayNight.NoActionBaralready 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 firstcountpicks 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:statusBarColorhas 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
- 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/setandroid: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.
- 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.
- Users in one locale saw a
Resources.NotFoundExceptionfrom the cart badge. - The translated
pluralsblock hadquantity="one"andquantity="few"(copied from another language) but noquantity="other". - That language's rules asked for
otherfor most counts, and with neither the required form norotherpresent,getQuantityStringthrew.
- Added the missing
<item quantity="other">to every translated plurals block. - Raised lint's
MissingQuantitycheck 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 avalues-night/variant. - Forgetting quantity="other" in a plurals block. Most languages use
otherfor many counts. Without it,getQuantityStringcan throwResources.NotFoundException. Always includeother, plus the forms each language needs. - Using density qualifiers to target tablets. Density does not mean form factor: a phone can be
xxhdpiand a tabletxhdpi. Usesw600dporw600dpfor 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 invalues-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 invalues-night/. Use aDayNightparent.
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
- Open
res/values/themes.xml. Note the name of your app theme (for exampleTheme.MyApp) and check that its parent is aDayNighttheme. - Create
res/values-night/themes.xml. Use the same theme name and parent, and changecolorPrimary,colorOnPrimary,colorSurface, andcolorOnSurfaceto dark values (for example#D0BCFF,#381E72,#1C1B1F,#E6E1E5). - In
res/values/strings.xml, add a<plurals name="photo_count">block withquantity="one"set to%d photoandquantity="other"set to%d photos. - In your main Activity or Compose screen, call
resources.getQuantityString(R.plurals.photo_count, 3, 3)(orpluralStringResourcein Compose) and log the result. - 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. - Add
res/values-fr/colors.xmlwith one colour also defined invalues-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.