Android Jetpack Compose - Implement Dark Mode
Last Updated :
20 Mar, 2025
Jetpack Compose is a new UI toolkit from Google that is used to create native Android UI. It speeds up and simplifies UI development by using less code, Kotlin APIs, and powerful tools.
Fortunately, Android 10 and later enable automatically "dark-theming" your app by forcing it to utilize certain darker hues. You may enable this system feature for your app by adding the
android:forceDarkAllowed="true"
To the theme of your choice. When this option is enabled, it will automatically evaluate your light theme and apply a dark version to it.
Now there are two problems with the above approach :
- What if the user wants dark mode for a specific app and not system-wide dark mode?
- How to implement seamless dark mode below android 10 using jetpack compose?
So now we will write a template that will help us enable dark mode on lower versions of android and also if the user wants to enable it for a specific app.
Steps to Implement Dark Mode
Step 1: Create a new android studio project
To create a new project in the Android Studio, please refer to How to Create a new Project in Android Studio with Jetpack Compose.
Step 2: Working with MainActivity.kt
Navigate to app > kotlin+java > {package-name} > MainActivity.kt and add the following code. In this file, we will be creating a custom theme and a switch to toggle between light and dark mode with respect to the custom theme.
MainActivity.kt:
Kotlin
package com.geeksforgeeks.demo
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val isDarkMode = remember { mutableStateOf(false) }
MyCustomTheme(isDarkMode) {
MyCustomScreen(isDarkMode)
}
}
}
}
@Composable
fun MyCustomTheme(isDarkMode: MutableState<Boolean>, content: @Composable () -> Unit) {
val darkColors = darkColorScheme(
primary = Color(0xFF00C853),
onPrimary = Color.Black,
background = Color(0xFF121212),
onBackground = Color.White,
surface = Color(0xFF121212),
onSurface = Color.White
)
val lightColors = lightColorScheme(
primary = Color(0xFF00C853),
onPrimary = Color.White,
background = Color.White,
onBackground = Color.Black,
surface = Color.White,
onSurface = Color.Black
)
val colors = if (isDarkMode.value) darkColors else lightColors
MaterialTheme(colorScheme = colors) {
Surface(
modifier = Modifier.fillMaxSize(),
color = colors.background
) {
content()
}
}
}
@Composable
fun MyCustomScreen(isDarkMode: MutableState<Boolean>) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Switch(
checked = isDarkMode.value,
onCheckedChange = { isDarkMode.value = it }
)
Text(
text = if (isDarkMode.value) "Dark Mode" else "Light Mode",
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onBackground,
modifier = Modifier.padding(top = 8.dp)
)
}
}
Output:
Similar Reads
Android Jetpack Compose - Implement Zoomable View Jetpack Compose is a new UI toolkit from Google used to create native Android UI. It speeds up and simplifies UI development using less code, Kotlin APIs, and powerful tools. Prerequisites:Familiar with Kotlin and OOP Concepts as wellBasic understanding of Jetpack ComposeAndroid Studio Canary Versio
9 min read
Android Jetpack Compose - Implement Easy Rating Dialog Many times in android applications we can get to see that they ask for users to rate their application and share their reviews about the application. In this article, we will take a look at How to implement an Easy Rating dialog in android applications using Jetpack Compose. Using the Easy Rating Di
4 min read
Toast in Android Jetpack Compose In Android, a Toast is a message or a pop-up message that generally appears at the bottom of the screen for a short span. A Toast is used to deliver simple feedback about any function or operation the application is running on the device. In simpler words, it displays the status of any running or fi
2 min read
Spacer in Android Jetpack Compose In Jetpack Compose, a Spacer is a blank element that is used to create a Space between two UI elements. Suppose, we have created Element 1 and we want to place Element 2 below Element 1 but with a top margin, we can declare a Spacer between the two elements. So in this article, we will show you how
2 min read
Android Jetpack Compose - Create a Movie App In the modern digital era, the demand for movie apps is soaring. These applications provide users with an immersive experience, allowing them to discover, explore, and enjoy a wide range of movies. Jetpack Compose, a declarative UI toolkit for Android, has gained immense popularity due to its simpli
8 min read