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.

What was checked, and how: read this first
Checked on the install (earlier work): that a generated project has 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:

<!-- app/src/main/res/values/strings.xml --> <resources> <string name="app_name">ComposeHello</string> <string name="greeting">Hello, %1$s!</string> <string name="open_details">Open details</string> </resources>
Text(text = stringResource(R.string.greeting, name)) Button(onClick = onOpen) { Text(stringResource(R.string.open_details)) }
  • %1$s is a placeholder for the first argument (general knowledge); stringResource(R.string.greeting, name) fills it in.
  • (docs, with the localization point from general knowledge) stringResource retrieves localized text. To add a translation, add a folder such as values-es with the same names and Android picks the right file for the user's language:
<!-- app/src/main/res/values-es/strings.xml (a Spanish translation) --> <resources> <string name="app_name">ComposeHello</string> <string name="greeting">Β‘Hola, %1$s!</string> <string name="open_details">Abrir detalles</string> </resources>

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.

<!-- res/values/dimens.xml --> <resources> <dimen name="screen_padding">16dp</dimen> </resources> // In a composable Column(modifier = Modifier.padding(dimensionResource(R.dimen.screen_padding))) { /* ... */ }

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:

// ui/theme/Color.kt (the shape of the generated file; the values are examples) val Purple80 = Color(0xFFD0BCFF) val Purple40 = Color(0xFF6650A4)
// ui/theme/Theme.kt (the shape of the generated file; yours may differ) private val DarkColorScheme = darkColorScheme(primary = Purple80) private val LightColorScheme = lightColorScheme(primary = Purple40) @Composable fun ComposeHelloTheme( darkTheme: Boolean = isSystemInDarkTheme(), dynamicColor: Boolean = true, content: @Composable () -> Unit ) { val colorScheme = when { dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { val context = LocalContext.current if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) } darkTheme -> DarkColorScheme else -> LightColorScheme } MaterialTheme( colorScheme = colorScheme, typography = Typography, content = content ) }
PartWhat it does
darkTheme = isSystemInDarkTheme()(docs) Follows the phone's light or dark setting by default.
lightColorScheme / darkColorSchemeBuild 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.

Text( text = "Headline", style = MaterialTheme.typography.headlineMedium, color = MaterialTheme.colorScheme.primary ) Card(shape = MaterialTheme.shapes.medium) { /* ... */ }

Seeing Dark Mode

In the preview: @PreviewLightDark shows light and dark at once (Android Studio 2: The New UI 5).

@PreviewLightDark @Composable fun GreetingPreview() { ComposeHelloTheme { Greeting("Android") } }
  • 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).
Hard-coded colours break dark mode
A composable that sets 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 codestringResource(R.string...)
Colours and stylescolors.xml, themes.xml, style attributesColor.kt and MaterialTheme in Kotlin
Dark modeA values-night folder and a DayNight themeA dark colour scheme chosen with isSystemInDarkTheme()
Reading a theme colour?attr/colorPrimaryMaterialTheme.colorScheme.primary
Translationsvalues-xx foldersThe same values-xx folders
Compared with the Classic UI course
Android Development Fundamentals 8 covers string, colour and dimension resources, themes and dark mode basics in XML. Resource folders and translations are unchanged; styling moves into Kotlin theme files and MaterialTheme.

Try It Yourself

Exercise 1

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.

πŸ“„ View solution
Exercise 2

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.

πŸ“„ View solution
Exercise 3

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.

πŸ“„ View solution
Not Yet Verified
Checked on the install (earlier work): the 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).
Coming up
Chapter 9, the last in the course, is the legacy appendix: why XML, Views and Fragments still exist, how to read an XML layout, and how ComposeView and AndroidView let the two worlds sit side by side.