Dark and Light Mode in Jetpack and Android
This tutorial delves into the implementation of light and dark mode using Jetpack and Kotlin when developing an android application.The switching is automatic depending on the device's theme.
Why Dark and Light Mode in Jetpack and Android?
Introduction
When it comes to Android app design, a straightforward decision like light vs dark mode can drastically alter the user experience. Developers are revolutionizing the way we interact with mobile UI by utilizing tools like Jetpack Compose and Kotlin at our disposal. Imagine a theme that can transition between light and dark with ease, adjusting to the user's tastes and environment. Thanks to the dynamic features of Jetpack Compose and the adaptability of Kotlin, this is the art of creating user-friendly apps that look amazing and feel precisely tailored to every user.
Let’s explore the wonders of Android development's light and dark modes and learn how these capabilities enable us to create interfaces that are not only functional but also visually appealing hence enhancing user experiences.
To implement dark mode or light practically,there are files that must be added to the application namely Color.kt and Theme.kt.These two files play an essential role in defining the required attributes for switching between themes.
Try Kodaschool for free
Click below to sign up and get access to free web, android and iOs challenges.
Color.kt
In most android application architectures using Jetpack Compose,Color.kt file usually resides in the ui/theme folder inside the main folder.This is where all the colors required by the UI are defined.
These color variables are essential for specifying how elements in the application's user interface (UI) look, as they provide uniformity and visual attractiveness in various interface modes, such as light and dark mode. The lighter tones (Purple40, PurpleGrey40, Pink40) offer contrast and readability against bright backdrops in light mode, while the deeper shades (Purple80, PurpleGrey80, Pink80) produce a muted and aesthetically pleasing interface in dark mode. The app achieves a unified design that easily adjusts to user preferences and environmental conditions by clearly specifying these colors, which improves overall usability and user experience.
Theme.kt
The code that causes an Android application to switch between light and dark mode is contained in this file.It includes dynamic color scheme selection (darkTheme) based on user preference and device compatibility. It uses predetermined values (DarkColorScheme and LightColorScheme) to set the main, secondary, and tertiary colors of the app.
Let walk through a step by step procedure creating a news app that leverages on Jetpack and Kotlin.
Requirements
- Latest Android Studio with an Emulator installed.
- Laptop(Windows,Linux or MacOs ) which should be 8GB RAM and above.
- An Android Phone with developer options enabled.
1.Start with an Empty Activity and define all the Required files that come with the default files such as MainActivity and Color.kt,Theme.kt and Type.kt that are located in the theme folder.

2.Define a SampleData.kt file that contains an array of items to display in the app.
SampleData.kt
package com.example.newsapp
/**
* SampleData for Jetpack Compose Tutorial
*/
object SampleData {
// Sample conversation data
val conversationSample = listOf(
Message(
"John",
"How are you?"
),
Message(
"John",
"""List of Global Leaders:
|Joe Biden (USA)
|Boris Johnson (UK)
|Narendra Modi (India)
|Angela Merkel (Germany)
|Xi Jinping (China)""".trim()
),
Message(
"John",
"""I think Spanish is a beautiful language.
|It's so rich and expressive!""".trim()
),
Message(
"John",
"Looking for updates on the new international space station project..."
),
Message(
"John",
"""Hey, did you hear about the new technology hub in Silicon Valley?
|It's bringing innovation and growth to the tech industry globally.
|Exciting times ahead!""".trim()
),
Message(
"John",
"The tech hub is part of a global initiative to boost technology :)"
),
Message(
"John",
"Investing in tech startups worldwide seems so promising, where do we start?"
),
Message(
"John",
"Apple's new features are incredible!"
),
Message(
"John",
"Mobile money platforms are leading the world ^_^"
),
Message(
"John",
"Did you know you can now access government services online in many countries?"
),
Message(
"John",
"The innovation in fintech globally is amazing, digital payments keep evolving"
),
Message(
"John",
"Interactive maps for city traffic are now available worldwide"
),
Message(
"John",
"Have you tried booking train tickets online in your country?"
),
)
}
3.Create a function called Conversation in MainActivity.kt file that will call the SampleData.kt a to display a list of conversation on local news.
@Composable
fun Conversation(messages: List<Message>) {
//Here messages is an instance of Message in the MessageCard function above
LazyColumn {
items(messages) { message ->
MessageCard(message)
}
}
}
//HAPPY CODING !!!
@Preview(name = "Light Mode")
@Preview(
uiMode = Configuration.UI_MODE_NIGHT_YES,
showBackground = true,
name = "Dark Mode"
)
@Preview(showBackground = true)
@Composable
fun MessageCardPreview() {
NewsAppTheme {
Surface() {
MessageCard(msg = Message("Wilson", "Hey, take a look at Jetpack Compose, it's great!"))
Conversation(SampleData.conversationSample)
}
}
}
@Preview
@Composable
fun PreviewConversation() {
NewsAppTheme {
Conversation(SampleData.conversationSample)
}
}
4.Define the various theme colors in the Color.kt file.
package com.example.newsapp.ui.theme
import androidx.compose.ui.graphics.Color
val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)
val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
5.Define the code in the Theme.kt file.
package com.example.newsapp.ui.theme
import android.app.Activity
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.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
private val DarkColorScheme = darkColorScheme(
primary = Purple80,
secondary = PurpleGrey80,
tertiary = Pink80
)
private val LightColorScheme = lightColorScheme(
primary = Purple40,
secondary = PurpleGrey40,
tertiary = Pink40
/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)
@Composable
fun NewsAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
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
}
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}
Light Mode

Dark mode

Conclusion
In conclusion, utilizing Jetpack Compose and Kotlin to achieve dark and light modes in your Android applications is a simple yet effective method. You can define different color schemes and easily transition between them based on the device settings with Jetpack Compose's declarative UI framework.
Utilizing Jetpack Compose's MaterialTheme and designing specific color schemes for dark and light modes will help you make sure your application adjusts dynamically and offers a dependable and aesthetically pleasing user experience. Furthermore, the interface is made even more unique by the use of dynamic colors, which are accessible starting with Android 12 onwards and reflect the theme and wallpaper choices of the device.
Happy coding!