By Shakil Ahmed, Android developer & software engineer
Material 3 Themes, Colors and Dark Mode in Jetpack Compose
Give your Android app its own brand colors, a custom font and automatic dark mode with Material 3 themes in Jetpack Compose. Beginner friendly.
Our fruit app works well now. It has a list, a details screen, a ViewModel, a top bar and bottom tabs. But it still uses the default purple colors from the template, and some text uses hard-coded gray. In this part we will learn about Material 3 themes and dark mode in Jetpack Compose. You will give your app its own brand colors, a custom font, and a dark mode that switches on by itself. If you missed the last lesson, read Part 10: Scaffold, Top App Bar and Bottom Navigation first.
What Is a Theme?
Think of a theme like a uniform for a school. Every student wears the same colors, so the school looks like one team. A theme does the same for your app. You choose the colors, fonts and shapes one time, and every screen uses them.
If you want to change your brand color later, you change it in one place. You do not need to search through 20 screens. ๐
The 3 Theme Files the Template Made
When you created the project with the "Empty Activity" template, Android Studio made a folder called ui/theme. Open it in the Project panel. You will see 3 files:
Color.ktโ your color values (likePurple80,Pink40).Theme.ktโ theMyFirstAppThemefunction. It picks the right colors for light or dark mode.Type.ktโ your text styles (font size, font weight, font family).
Remember MainActivity? It wraps everything inside MyFirstAppTheme { ... }. That is why every composable inside it can use the theme.
MaterialTheme.colorScheme and MaterialTheme.typography
Inside any composable, you can ask the theme for values:
MaterialTheme.colorScheme.primaryโ the main brand color.MaterialTheme.colorScheme.onPrimaryโ the color for text on top of primary.MaterialTheme.colorScheme.surfaceโ the background of cards and screens.MaterialTheme.colorScheme.onSurfaceVariantโ a softer text color, good for small details.MaterialTheme.typography.titleLarge,bodyMedium,labelSmallโ ready-made text styles.
The word "on" is important. onPrimary means "the color that is easy to read on primary". Material 3 always gives you matching pairs, so your text never disappears into the background.
Step 1: Choose Your Brand Colors
Let's make our fruit app green, like fresh leaves. ๐
Tip: Picking good colors is hard. Use the free Material Theme Builder website. Choose one main color, and it makes a full light and dark color set for you. It can even export a Jetpack Compose theme.
Open ui/theme/Color.kt and replace everything with this:
package com.example.myfirstapp.ui.theme
import androidx.compose.ui.graphics.Color
// Light mode colors
val Green40 = Color(0xFF2E7D32)
val Orange40 = Color(0xFFE65100)
val Leaf40 = Color(0xFF558B2F)
// Dark mode colors (lighter, so they look good on a dark background)
val Green80 = Color(0xFFA5D6A7)
val Orange80 = Color(0xFFFFB74D)
val Leaf80 = Color(0xFFC5E1A5)
Let's understand it:
Color(0xFF2E7D32)makes a color from a hex code.0xFFmeans "fully visible".2E7D32is the green.- We make two versions of each color. Dark colors for light mode, light colors for dark mode.
- If your package name is different, keep your own first line.
Step 2: Update Theme.kt
Open ui/theme/Theme.kt. Replace the whole file with this:
package com.example.myfirstapp.ui.theme
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
private val LightColorScheme = lightColorScheme(
primary = Green40,
secondary = Leaf40,
tertiary = Orange40
)
private val DarkColorScheme = darkColorScheme(
primary = Green80,
secondary = Leaf80,
tertiary = Orange80
)
@Composable
fun MyFirstAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = false,
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,
shapes = AppShapes,
content = content
)
}
Let's understand it:
lightColorScheme(...)anddarkColorScheme(...)build full color sets. You only give the colors you want to change. Material 3 fills in the rest.isSystemInDarkTheme()checks the phone setting. If the user turned on dark mode, it returnstrue. So dark mode works automatically.dynamicColoris about a special Android 12+ feature (see below). We changed the default fromtruetofalse.Build.VERSION_CODES.Smeans Android 12.shapes = AppShapesis new. We will createAppShapesin Step 5. Until then, this line will be red. That is normal.
What Is Dynamic Color?
On Android 12 and newer, the phone can make colors from the user's wallpaper. If the wallpaper is blue, apps become blue. This is called dynamic color.
It looks nice. But it also means your brand colors are ignored on new phones. That is why many beginners say, "I changed my colors, but nothing happened!" The answer: dynamic color was on.
dynamicColor: Boolean = falseโ your colors always show. โdynamicColor: Boolean = trueโ wallpaper colors on Android 12+, your colors on older phones.
Both are fine. Choose what you like. For learning, keep it false so you can see your changes.
Step 3: Use Theme Colors Instead of Color.Gray
In Part 7 we wrote the price text in Color.Gray. Gray looks OK in light mode, but in dark mode it can be hard to read. Let's use a theme color instead. Here is the full FruitRow:
@Composable
fun FruitRow(fruit: Fruit, onClick: () -> Unit) {
Card(
onClick = onClick,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 6.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant
)
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = fruit.emoji, fontSize = 32.sp)
Spacer(modifier = Modifier.width(16.dp))
Column {
Text(
text = fruit.name,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onSurface
)
Text(
text = "เงณ${fruit.price} per kg",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.primary
)
}
}
}
}
Let's understand it:
CardDefaults.cardColors(containerColor = ...)sets the card background from the theme.- The fruit name uses
onSurface, so it is readable in both modes. - The price now uses
primaryโ our green. It changes to light green in dark mode by itself. style = MaterialTheme.typography.titleMediumuses a ready-made text size. No more guessingfontSizenumbers.
If you see red words, click on them and press Alt+Enter to import. Tricky imports:
androidx.compose.material3.CardDefaultsandroidx.compose.material3.MaterialThemeandroidx.compose.ui.text.font.FontWeight
Tip: Search your project for
Color.Gray,Color.BlackandColor.White(press Ctrl+Shift+F). Each one is a place where dark mode might look wrong. Replace them with theme colors.
Step 4: Preview Light and Dark Side by Side
You don't need to change your phone setting every time. You can preview both modes in Android Studio:
@Preview(name = "Light", showBackground = true)
@Preview(
name = "Dark",
showBackground = true,
uiMode = Configuration.UI_MODE_NIGHT_YES
)
@Composable
fun FruitRowPreview() {
MyFirstAppTheme {
Surface {
FruitRow(
fruit = Fruit("Mango", "๐ฅญ", 120),
onClick = {}
)
}
}
}
Let's understand it:
- You can put two
@Previewlines on one function. You get two previews. uiMode = Configuration.UI_MODE_NIGHT_YEStells the preview "pretend dark mode is on".- We wrap the row in
MyFirstAppThemeandSurface.Surfacepaints the correct background color. - Import
Configurationfromandroid.content.res.Configuration(press Alt+Enter and choose this one).
Open the Split view at the top right of the editor. You will see a light card and a dark card. ๐
Tip: If your
Fruitclass now has more fields (for example from later parts), add them in the preview too.
Step 5: A Custom Font
Default fonts are fine, but a custom font gives your app personality. Let's use Poppins, a free font from Google Fonts.
- Go to fonts.google.com, search Poppins, and download it.
- Unzip the file. Find
Poppins-Regular.ttfandPoppins-Bold.ttf. - Rename them to
poppins_regular.ttfandpoppins_bold.ttf. Android resource names must be lowercase, with only letters, numbers and underscores. - In Android Studio, right-click the
resfolder โ New โ Android Resource Directory. Choose resource type font and click OK. - Copy the two
.ttffiles intores/font.
Now open ui/theme/Type.kt and replace it with this:
package com.example.myfirstapp.ui.theme
import androidx.compose.material3.Typography
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import com.example.myfirstapp.R
val Poppins = FontFamily(
Font(R.font.poppins_regular, FontWeight.Normal),
Font(R.font.poppins_bold, FontWeight.Bold)
)
private val default = Typography()
val Typography = Typography(
titleLarge = default.titleLarge.copy(fontFamily = Poppins),
titleMedium = default.titleMedium.copy(fontFamily = Poppins),
bodyLarge = default.bodyLarge.copy(fontFamily = Poppins),
bodyMedium = default.bodyMedium.copy(fontFamily = Poppins),
labelLarge = default.labelLarge.copy(fontFamily = Poppins)
)
Let's understand it:
FontFamily(...)groups the regular and bold files into one family.Typography()with no arguments gives the default Material 3 styles. We take them and only change the font with.copy(fontFamily = Poppins).- Every
Textthat usesMaterialTheme.typography.bodyMedium(and the others) now shows Poppins. Rmust be your app'sR. Changecom.example.myfirstapp.Rto your package name.
Step 6: Shapes (Quick Look)
Shapes control how round the corners are on cards, buttons and dialogs. Create a new file ui/theme/Shape.kt:
package com.example.myfirstapp.ui.theme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Shapes
import androidx.compose.ui.unit.dp
val AppShapes = Shapes(
small = RoundedCornerShape(8.dp),
medium = RoundedCornerShape(16.dp),
large = RoundedCornerShape(24.dp)
)
Now the red AppShapes line in Theme.kt is fixed. Cards use the medium shape, so all your fruit cards get rounder 16.dp corners. Want sharp corners everywhere? Change the numbers to 0.dp.
Step 7: Test Dark Mode on Your Phone
Run the app. Then:
- Pull down the notification shade on your phone or emulator.
- Tap Dark theme (or go to Settings โ Display โ Dark theme).
- Go back to your app.
The app switches to dark colors by itself. The price turns light green, the cards turn dark. You wrote zero extra code for this. ๐
Common Errors
- "I changed colors but the app looks the same." Dynamic color is on and you are on Android 12+. Set
dynamicColor: Boolean = falseinTheme.kt. - "Unresolved reference: poppins_regular." The file name has capital letters or a dash. Rename it to lowercase with underscores, then use Build โ Rebuild Project.
- "Unresolved reference: R" in Type.kt. Import your own app's
Rclass, likecom.example.myfirstapp.R, notandroid.R. - Text is black on a dark background. Somewhere you still use a hard-coded
Color.Black, or the content is not inside aSurfaceorScaffold. Use theme colors. - "Unresolved reference: Configuration." Import
android.content.res.Configuration.
Try It Yourself
- Use the Material Theme Builder to pick an orange brand color, export the colors, and paste them into
Color.kt. - Set
dynamicColor = true, change your phone wallpaper, and see your app change color. - Make the fruit name on
FruitDetailsScreenuseMaterialTheme.typography.headlineMediumandcolorScheme.primary.
Frequently Asked Questions
Can I force my app to always be dark?
Yes. In MainActivity, call MyFirstAppTheme(darkTheme = true) { ... }. But most users prefer the app to follow their phone setting.
Should I keep dynamic color on or off? Off is good if your brand color matters (like a shop app). On is nice for simple tools and feels personal. Both are correct.
Why do dark mode colors need to be lighter? Dark green text on a black background is hard to read. Lighter colors have better contrast on dark backgrounds, so your text stays clear.
Quick Recap
- The theme lives in
ui/theme:Color.kt,Theme.kt,Type.kt(and our newShape.kt). - Use
MaterialTheme.colorSchemeandMaterialTheme.typographyinstead of hard-coded colors and sizes. lightColorSchemeanddarkColorSchemehold your brand colors.- Dynamic color uses wallpaper colors on Android 12+. Turn it off to see your own colors.
isSystemInDarkTheme()makes dark mode automatic.@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)previews dark mode.- Custom fonts go in
res/fontwith lowercase names.
Our app looks beautiful now. Next, let's replace those emojis with real fruit photos from the internet.
Part 12: Load Images from the Internet in Jetpack Compose with Coil
Related articles
Diagnose a faded Material 3 card, pair its foreground and background colors, and check the result in both light and dark mode.
Pick a photo, read its text on-device with ML Kit Text Recognition v2, and show blocks and lines in Jetpack Compose with a copy button.
Run AI directly on the phone with Gemini Nano and ML Kit GenAI APIs: privacy, offline use, device support, a Compose summarizer, cloud fallback and limits.
Build a secure AI chat in your Android app with Firebase AI Logic and Gemini: setup, App Check, model choice, streaming ViewModel, Compose UI and error handling.