One of the most common challenges Android developers face when working with Kotlin is the issue of dark mode not applying theme properly. Even though Android supports day/night themes natively, many apps still struggle with incorrect colors, improper resource loading, or activities that ignore dark mode altogether. In this article, we will explore the reasons behind this issue, provide step-by-step fixes, and share complete Kotlin code examples you can implement immediately.

Why Dark Mode Theme Does Not Apply Properly
Dark mode is a popular feature in Android apps, but it requires proper configuration. Common reasons why your Kotlin Android app does not apply the dark theme properly include:
- Incorrect theme inheritance in
styles.xml
- Not forcing dark mode support programmatically
- Using hardcoded colors instead of theme attributes
- Improperly overriding
AppCompatDelegate
in activities or fragments - Custom views that don’t respect the night mode attributes
Step 1: Ensure Correct Theme Setup
Check that your styles.xml
includes the correct parent theme with DayNight support. For example:
<resources> <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> </style> </resources>
If you are using a theme that does not inherit from DayNight
, the dark mode will not apply automatically.
Step 2: Enable Dark Mode Programmatically
Sometimes, the app does not detect the system dark mode setting. You can force it by using AppCompatDelegate
in Kotlin:
import androidx.appcompat.app.AppCompatDelegate class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Force dark mode AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) // Or follow system theme // AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) setContentView(R.layout.activity_main) } }
This ensures that your app respects the system setting or forces dark mode depending on your requirements.
Step 3: Use Theme Attributes Instead of Hardcoded Colors
If you use hardcoded colors (e.g., #FFFFFF
for text or #000000
for background), the dark mode will fail to adapt. Instead, use theme attributes:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dark Mode Test" android:textColor="?attr/colorOnPrimary" android:background="?attr/colorPrimary" />
This ensures that your UI elements automatically adapt when the theme switches to night mode.
Step 4: Handling Custom Views in Dark Mode
If you are building custom views, you need to make sure you reference theme attributes and not fixed colors. For example:
class CustomCardView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : CardView(context, attrs) { init { setCardBackgroundColor( MaterialColors.getColor(context, com.google.android.material.R.attr.colorSurface, Color.WHITE) ) } }
This ensures that your custom view respects the dark theme automatically.
Step 5: Verify With Configuration Change
When switching themes, you may need to recreate your activity so that all resources are applied correctly:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) recreate()
This ensures that all layouts and colors are reloaded with the new theme.
Example: Toggle Dark Mode With a Button
Here is a full Kotlin example where the user can toggle between dark mode and light mode:
class SettingsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_settings) val switchTheme = findViewById<Switch>(R.id.switchTheme) switchTheme.setOnCheckedChangeListener { _, isChecked -> if (isChecked) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) } recreate() } } }
Comparison Table: Common Issues vs Solutions
Problem | Cause | Solution |
---|---|---|
Dark mode not applying | Theme not using DayNight | Use Theme.MaterialComponents.DayNight |
Colors not adapting | Hardcoded colors | Use theme attributes like ?attr/colorOnPrimary |
Activity ignores theme | No AppCompatDelegate usage | Set MODE_NIGHT_YES/NO/FOLLOW_SYSTEM |
Custom views look wrong | Using fixed color values | Use MaterialColors.getColor() |
Conclusion
Fixing the Kotlin dark mode not applying theme properly issue requires a proper combination of correct theme inheritance, programmatic configuration, and reliance on theme attributes instead of hardcoded colors. By following the steps above and applying the complete Kotlin code examples, your Android application will seamlessly support light and dark modes, improving user experience and aligning with modern Android UI design practices.
For further reading, check out the official Android Dark Theme documentation.