Resources and Themes
Android Development Fundamentals with Compose
Course 4 Β· Chapter 8 Β· Words, Colours and Dark Mode
So far the text in our composables has been written straight into the code, and every colour has been whatever the theme gave us. Real apps do two things better. They keep user-visible text in resources, so it can be translated, and they
keep colours, fonts and shapes in a theme, so the whole app can change look (including dark mode) in one place. You met the theme files in Chapter 1 (ui/theme/); this chapter opens them.
ui/theme/Color.kt, Theme.kt and Type.kt, and a res/values folder. Checked in the docs (two web searches, summaries only): stringResource, dimensionResource and colorResource;
isSystemInDarkTheme() choosing between light and dark colour schemes; dynamic colour on Android 12 and above with dynamicDarkColorScheme and dynamicLightColorScheme; MaterialTheme taking colorScheme, typography and shapes.
General knowledge, not checked: translation folders such as values-es, formatted strings, and the exact contents of your generated theme files (the listings are a shape). All code is unrun (no Kotlin compiler on the machine used to write this).
Resources: Keep Text Out of the Code
The res/values folder holds XML files of simple values. The most important is strings.xml. (docs) In Compose you read a string resource with stringResource:
%1$sis a placeholder for the first argument (general knowledge);stringResource(R.string.greeting, name)fills it in.- (docs, with the localization point from general knowledge)
stringResourceretrieves localized text. To add a translation, add a folder such asvalues-eswith the same names and Android picks the right file for the user's language:
Resource Manager (the left-hand tool window bar, Android Studio 2: The New UI 1) lists and previews your resources (docs describe it; I have not used it here).
Dimensions and Colours
(docs) dimensionResource reads a value from a dimension XML file, and colorResource reads a colour; the docs describe colorResource as the tool for adopting Compose step by step, and note that it flattens colour state lists.
(general knowledge) In a pure Compose app most people write sizes as 16.dp in code and take colours from the theme, so these two are less common than stringResource.
The Theme Files
The three files in ui/theme/ work together: Color.kt defines raw colours, Type.kt defines the Typography (text styles), and Theme.kt turns them into a MaterialTheme that you wrap around the app in setContent (Chapter 1).
Compare your generated files with these shapes:
| Part | What it does |
|---|---|
darkTheme = isSystemInDarkTheme() | (docs) Follows the phone's light or dark setting by default. |
lightColorScheme / darkColorScheme | Build a Material 3 colour scheme from your colours (general knowledge). |
dynamicColor | (docs) Dynamic colour builds the palette from the wallpaper on Android 12 and above; otherwise your own schemes are used. |
MaterialTheme(colorScheme, typography, ...) | (docs) Makes the scheme, text styles and shapes available to everything inside. |
Using the Theme in Your Composables
(docs) Read the current values from MaterialTheme.colorScheme, MaterialTheme.typography and MaterialTheme.shapes, rather than hard-coding a colour or size. The text and the card then change automatically when the theme does.
Seeing Dark Mode
In the preview: @PreviewLightDark shows light and dark at once (Android Studio 2: The New UI 5).
- On a device: switch the emulator or phone to dark theme in its system settings, and the running app follows it because of
isSystemInDarkTheme()(general knowledge: where the setting sits varies by Android version).
Color.White or Color.Black directly ignores the theme, so text can vanish against the opposite background. Use MaterialTheme.colorScheme entries instead (general knowledge).
Compared with the XML Way
| Views (Android Development Fundamentals) | Compose | |
|---|---|---|
| Text in resources | @string/... in layouts, getString in code | stringResource(R.string...) |
| Colours and styles | colors.xml, themes.xml, style attributes | Color.kt and MaterialTheme in Kotlin |
| Dark mode | A values-night folder and a DayNight theme | A dark colour scheme chosen with isSystemInDarkTheme() |
| Reading a theme colour | ?attr/colorPrimary | MaterialTheme.colorScheme.primary |
| Translations | values-xx folders | The same values-xx folders |
MaterialTheme.
Try It Yourself
Move the text of your Home and Detail screens (Chapter 7) into strings.xml and read it with stringResource, including one string with a placeholder. Then add a values-es translation (or another language you know) and switch the device language to see it.
Open Color.kt and Theme.kt in your project and write down what they contain. Change the primary colour in both the light and dark schemes, turn dynamicColor off, and compare the preview before and after.
Preview a screen with @PreviewLightDark. Deliberately hard-code Color.Black on a Text and describe what happens in the dark preview. Fix it with a theme colour.
ui/theme files and the res/values folder exist in a generated project.Checked in the docs (two web searches, summaries only):
stringResource, dimensionResource, colorResource (and its flattening of colour state lists); isSystemInDarkTheme(); dynamic colour (Android 12+); MaterialTheme parameters and the MaterialTheme.colorScheme, typography, shapes accessors; Resource Manager.General knowledge, not checked:
values-es translations and formatted strings (%1$s); the contents of your generated theme files; lightColorScheme and darkColorScheme details; the XML comparison column; where the device's dark-mode setting sits.Not verified: the imports; every listing and solution (unrun); the exercises (not run by me). Nothing in the app was checked on your install beyond the file list.
Sources: Resources in Compose (developer.android.com), Material Design 3 in Compose (developer.android.com), Theming in Compose with Material 3 (developer.android.com codelab), Manage your app's UI resources with Resource Manager (developer.android.com).
ComposeView and AndroidView let the two worlds sit side by side.