Resources & Configuration Flashcards

ANDROID › System

What is the difference between dp and px, and what is the conversion formula?
dp is a density-independent unit anchored to a 160 dpi baseline; px is a physical pixel. px = dp * (dpi / 160). So 1 dp equals 1 px on an mdpi (160 dpi) screen and 2 px on xhdpi (320 dpi).
When should you use sp instead of dp?
Use sp for font sizes. sp behaves like dp but also scales with the user's system font-size accessibility setting, so text respects the user's preferred scale. Use dp for all other dimensions (margins, padding, view sizes).
Why must you always provide default (unqualified) resources?
They are the fallback when no qualified directory matches the device configuration. Without them the app can throw a Resource Not Found exception and crash, for example when the device locale is one you did not provide a values- folder for.
Describe how Android selects the best-matching resource for a configuration.
It eliminates directories whose qualifiers contradict the device config, then walks the qualifier precedence table top to bottom; at the first qualifier present in any surviving directory it keeps only directories that include it, and repeats until one directory remains. Higher-precedence qualifiers (like locale) win over more numerous lower-precedence matches.
How is screen density treated differently from other qualifiers during selection?
Density is never eliminated as a contradiction. Android keeps all density variants and picks the closest bucket, scaling the bitmap to the device density (preferring to scale down). This lets one drawable serve any screen even if the exact bucket is missing.
What are the standard density buckets and their dpi/scale ratios?
ldpi ~120, mdpi ~160 (baseline, 1x), hdpi ~240 (1.5x), xhdpi ~320 (2x), xxhdpi ~480 (3x), xxxhdpi ~640 (4x) — a 3:4:6:8:12:16 ratio. nodpi assets are never scaled; anydpi (used for vector XML) is preferred regardless of density.
What is the smallestWidth (swNdp) qualifier and why is it preferred for tablet layouts?
swNdp matches the smallest of the screen's width and height in dp, which is constant regardless of orientation. res/layout-sw600dp targets devices with at least 600 dp in their shortest dimension (typical 7-inch+ tablets), giving a stable, orientation-independent breakpoint.
What are window size classes and what are the standard width breakpoints?
Window size classes are opinionated breakpoints over the app's current window (not the physical device) for choosing layouts. Standard width classes: Compact < 600 dp, Medium 600–839 dp, Expanded >= 840 dp. They are exposed in Compose via WindowSizeClass / currentWindowAdaptiveInfo and adapt correctly in multi-window and on foldables.
What is the Configuration object and how do you correctly observe configuration changes in Compose?
Configuration (from Resources.getConfiguration()) describes the current device config: locale, orientation, uiMode/night flag, screenWidthDp/screenHeightDp, smallestScreenWidthDp, densityDpi, fontScale. In Compose read it via LocalConfiguration.current rather than caching it, so recomposition tracks changes; prefer window size classes for layout decisions.

Back to Resources & Configuration