In Android's View system, how does a style differ from a theme, and why do DayNight layouts reference colours through ?attr/ rather than @color/?
answer
- one view versus a hierarchy
- style attribute does not cascade
- semantic names the theme fills in
- a uiMode configuration change
basics
~20 sA style sets attributes on the one view it is applied to; a theme names resources for a whole app, activity or view subtree. ?attr/ references resolve against the current theme, so DayNight can swap them.
solid answer
~40 sIn the View system a **style** is a bundle of view attributes applied with `style="@style/..."`; only that element gets them, not its children. A **theme** is applied with `android:theme` on `<application>`, an `<activity>` or (since API 21) a view, where it covers the whole subtree, and it gives semantic names such as `colorPrimary` or `colorSurface` their values. A layout or style that writes `?attr/colorSurface` asks the current theme at inflation time, whereas `@color/surface_light` is one fixed colour. With a `Theme.AppCompat.DayNight` or `Theme.MaterialComponents.DayNight` parent, the night variant supplies different values, so the same layouts and styles work in both modes. Switching mode is a `uiMode` configuration change, which recreates the activity by default.
code
xml · 21 lines<!-- res/values/themes.xml -->
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/brand</item>
</style>
<style name="ArticleCard">
<item name="android:background">?attr/colorSurface</item>
<item name="android:padding">16dp</item>
</style>
</resources>
<!-- res/values/colors.xml -->
<resources>
<color name="brand">#1565C0</color>
</resources>
<!-- res/values-night/colors.xml -->
<resources>
<color name="brand">#90CAF9</color>
</resources>go deeper
Recall that a style targets one view and a theme targets an app, activity or subtree, and that DayNight plus values-night gives a dark theme.
Explain ?attr/ resolution against the current theme, why a style does not cascade, and the uiMode configuration change behind a theme switch.
Debug a colour that will not change by walking the precedence list, and remove hardcoded colours and drawables that break dark mode in widgets and launch screens.
Own the theme as the app's design-token layer: semantic attributes only, no raw colours in layouts, and a review check that new screens work in both modes.
## Two tools with the same shape In Android's View system, styles and themes are both declared as `<style>` elements in `res/values/` (usually `styles.xml` or `themes.xml`), and both are key-value maps from attributes to resources. They differ in **where they are applied and what they reach**. | | Style | Theme | |---|---|---| | Applied with | `style="@style/Headline"` on one view | `android:theme` on `<application>`, `<activity>`, or a view | | Reaches | Only that element; children are untouched | Every view in the app, activity or subtree | | Typical content | Attributes of one view type (text colour, padding) | Semantic names (`colorPrimary`, `colorSurface`) and window attributes (`android:windowBackground`) | | Can style non-views | No | Yes, such as the status bar and window background | A view applies only the attributes it supports and ignores the rest, so a style written for a `TextView` and put on a `LinearLayout` silently does nothing to the text inside it. To push values into children, apply a **theme** to the parent with `android:theme`; since Android 5.0 (API 21) that works on any view and covers its whole subtree. ## Inheritance A style names its parent with `parent="TextAppearance.AppCompat"`, or, for your own styles only, with dot notation such as `GreenText.Large`. If both are used, the `parent` attribute wins. Core app styles should extend the AppCompat or Material Components versions, which smooth over differences between platform releases. Version-specific tweaks go in qualified files such as `res/values-v21/styles.xml` that extend a shared base theme. ## Theme attributes: ?attr/ versus @color/ A **theme attribute** reference starts with a question mark: - `?attr/colorSurface`, `?attr/colorOnSurface`, `?attr/colorControlNormal` for library attributes - `?android:attr/textColorPrimary`, `?android:attr/colorBackground` for platform attributes It means "whatever the current theme says". A direct reference such as `@color/white` is one fixed resource. That is why the dark-theme guidance says to avoid hardcoded colours and use theme attributes or night-qualified resources: a style that asks for `?attr/colorSurface` keeps working when the theme behind it changes, and you never edit the style. ## DayNight To support dark theme (available from Android 10, API 29), make the app theme inherit from a DayNight parent: 1. `Theme.AppCompat.DayNight` or `Theme.MaterialComponents.DayNight` as the `parent` of `AppTheme`. 2. Put dark values in `values-night/` (for example `colors.xml`), keeping light values in `values/`. 3. Reference those colours through theme attributes in layouts and styles. For an in-app Light, Dark or System default choice, map the options to `MODE_NIGHT_NO`, `MODE_NIGHT_YES` and `MODE_NIGHT_FOLLOW_SYSTEM`. On API 31 and above, call `UiModeManager#setApplicationNightMode` so the system can match the splash screen; on API 30 and below, call `AppCompatDelegate.setDefaultNightMode()`, which from AppCompat 1.1.0 recreates started activities itself. A theme switch is a **`uiMode` configuration change**, so the activity is recreated and views inflate against the new values. An activity that declares `android:configChanges="uiMode"` is kept instead and receives `onConfigurationChanged()`, and then it must reapply colours itself. **Force Dark** (`android:forceDarkAllowed="true"`) is a separate shortcut that recolours a light theme automatically; it is not applied when the theme already inherits from DayNight. ## Which value wins When the same attribute is set in several places, the documentation's order from highest to lowest precedence is: 1. Text spans on a `TextView`. 2. Attributes set in code. 3. Attributes set directly on the view in XML. 4. A style applied to the view. 5. Default styling. 6. A theme applied to the app, activity or view group. 7. View-specific styling such as `TextAppearance`. So a style overrides the theme for matching attributes on that view, while theme attributes it does not override still apply. When a colour "will not change", look for something higher in this list. ## Mistakes that break dark mode The dark-theme guidance lists the pitfalls that show up most often, especially on surfaces the app does not fully control, such as launcher widgets and custom notification views: - Assuming the background is always light, then setting dark text on it. - Hardcoding a text colour, or a background colour while leaving the default text colour. - Using a drawable icon with one fixed colour instead of tinting it with `?attr/colorControlNormal`. - Setting a launch-screen background to white in code instead of using `?android:attr/colorBackground`. - Expecting a style on a container to recolour its children, when only a theme overlay does that. Each fix is the same move: ask the theme for a semantic attribute, and let the DayNight parent and `values-night/` decide the value.
- Why does a style set on a LinearLayout not change the text colour of its TextViews?A style applies only to the element it is set on, and a `LinearLayout` ignores `android:textColor` because it does not support it. To change the children, apply a theme overlay with `android:theme` on the `LinearLayout`, or give each `TextView` the style or a `TextAppearance`.
- How should an in-app Dark option be applied on current Android releases?Map it to `MODE_NIGHT_YES`. On API 31 and above call `UiModeManager#setApplicationNightMode` so the system can match the splash screen; on API 30 and below call `AppCompatDelegate.setDefaultNightMode()`, which recreates started activities from AppCompat 1.1.0. Offer System default as the default choice.
A style is a paint order for one wall: "use the accent colour". The theme is the house's colour scheme that says what "accent" means; change the scheme for evening and every wall that asked for "accent" changes, while a wall painted a fixed colour code does not.
- A style on a parent layout is inherited by all its child views.
- Hardcoded @color/white backgrounds are fine because DayNight inverts them.
- Switching to dark theme repaints views without recreating the activity.
- Force Dark should be layered on top of a DayNight theme.
- Attributes set directly on a view lose to the activity's theme.
Assumes Android 17 (API 37) as the target; UiModeManager#setApplicationNightMode exists from API 31, so devices on API 30 and below still need AppCompatDelegate.setDefaultNightMode().