Modern Android sample showcasing a polished light/dark/system theme switcher built fully with Jetpack Compose, Material 3, MVVM, StateFlow, and coroutine-friendly DataStore persistence.
- π§© Full Jetpack Compose UI: No XML screen or DataBinding layer; the main screen is rendered with composable functions.
- π¨ Material 3 visual polish: Dynamic colors on Android 12+, gradient hero card, rounded surfaces, animated selected-theme feedback, and icon-led option rows.
- π§ MVVM state management:
MainViewModelexposes a singleThemeUiStateviaStateFlowand the UI collects it lifecycle-safely. - π Three theme modes: Light, Dark, and System Default modes are represented with a type-safe
ThemeModeenum. - πΎ Modern persistence: Theme preference is stored with AndroidX DataStore instead of SharedPreferences.
- π Edge-to-edge experience:
ComponentActivityenables edge-to-edge rendering and handles safe drawing insets in Compose.
| Area | Choice |
|---|---|
| Language | Kotlin |
| UI | Jetpack Compose, Material 3, Material Icons |
| Architecture | MVVM, ViewModel, StateFlow, lifecycle-aware Compose collection |
| Theme engine | Compose MaterialTheme, Material You dynamic color on Android 12+ |
| Persistence | AndroidX DataStore Preferences |
| Minimum SDK | 28 |
| Target SDK | 34 |
| Java target | 17 |
app/src/main/java/com/halil/ozel/darkmode/
βββ MainActivity.kt # Compose entry point, screen composables, Material theme setup
βββ MainViewModel.kt # MVVM state holder and theme selection actions
βββ ThemeMode.kt # Type-safe Light/Dark/System theme model
βββ ThemePreferences.kt # DataStore-backed theme persistence
app/src/main/res/
βββ values/themes.xml # Minimal NoActionBar light app theme for Compose host activity
βββ values-night/themes.xml # Minimal NoActionBar night app theme for Compose host activity
βββ drawable/ # Existing image/screenshot assets
MainActivitystarts as a Compose-onlyComponentActivity, enables edge-to-edge rendering, and callssetContent.MainViewModelobservesThemePreferences.themeModeand maps it intoThemeUiState.- The Compose UI collects
ThemeUiStatewithcollectAsStateWithLifecycle. - The selected
ThemeModedecides whetherDarkModeThemeuses light colors, dark colors, or the current system theme. - On Android 12 and newer, the app uses Material You dynamic color schemes; older devices use the fallback Material 3 color schemes.
- When the user taps a theme row,
MainViewModel.selectTheme()persists the new value with DataStore and the UI updates reactively.
- Android Studio with Jetpack Compose support
- JDK 17
- Android SDK Platform 34
- Network access to resolve Gradle, Android Gradle Plugin, Compose, Lifecycle, and DataStore dependencies if they are not already cached locally
./gradlew assembleDebug./gradlew installDebug./gradlew test
./gradlew lintExisting screenshot assets are stored in app/src/main/res/drawable/. Re-capture them after installing the Compose version so README visuals match the latest UI.
- π Move UI strings into
strings.xmlfor easier localization and translation workflows. - π§± Split composables into feature-specific files as the screen grows.
- π§ͺ Add Compose UI tests for theme selection and DataStore persistence.
- π Add preview variants for light, dark, and system theme states.
MIT License
Copyright (c) 2023 Halil OZEL
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.