Deep Dive: Resource Qualifiers
Android's resource system is designed to provide alternative assets dynamically based on the current device configuration. This allows a single APK to run seamlessly across devices with different screen sizes, languages, and hardware capabilities.
Screen Density Qualifiers
To ensure graphics remain crisp on various displays without consuming unnecessary memory, Android uses density qualifiers. Instead of physical pixels, layouts use dp (density-independent pixels), and the system loads the closest matching bitmap asset.
mdpi: Medium density (~160 dpi). The baseline.hdpi: High density (~240 dpi, 1.5x scaling).xhdpi: Extra-high density (~320 dpi, 2.0x scaling).xxhdpi: Extra-extra-high density (~480 dpi, 3.0x scaling).xxxhdpi: Extra-extra-extra-high density (~640 dpi, 4.0x scaling, typically reserved for launcher icons).
If a specific density is missing, Android will scale an existing resource, preferring to scale down from a higher density rather than upscaling (which causes blurring).
Language and Locale Qualifiers
Localization is handled by appending the ISO language and region codes to the values directory.
values-en/strings.xml: English (all regions).values-fr-rCA/strings.xml: Canadian French.values-b+zh+Hant/strings.xml: Traditional Chinese (using BCP 47 tags).
At runtime, the AssetManager constructs a fallback tree. If a string is not found in Canadian French, it falls back to generic French, and finally to the default values/strings.xml.
Night Mode, Layout Direction, and Size Qualifiers
Modern Android features rely heavily on these specialized qualifiers:
- Night Mode:
values-nightfor Dark Theme colors andvalues-notnightfor Light Theme. - Layout Direction:
layout-ldrtlprovides alternate layouts for Right-to-Left languages like Arabic and Hebrew, mirroring the UI structure. - Screen Size:
layout-sw600dp(smallest width 600dp) is the standard qualifier for tablets, ensuring that a multi-pane layout is only loaded when the device has sufficient screen real estate regardless of orientation.
Configuration Change Handling
When a configuration changes (e.g., the user rotates the device, changing the active qualifier from port to land), the Android framework, by default, destroys and recreates the current Activity. This ensures the UI is reinflated using the newly appropriate resources.
Developers can opt-out of this behavior by declaring android:configChanges in the manifest, but doing so means they must manually update the UI to reflect the new resource states, bypassing the automatic AssetManager resolution.