how to I implement two dark modes in android?

36 Views Asked by At

I am trying to implement dark and light mode in my application, I want to have two dark modes one with grey background and the other with black background for OLED displays. Following is the code I am using to change modes.

when (changeThemeTextView.editText?.text.toString()) {
    getString(R.string.System) -> {
        themeBackground = getString(R.string.System)
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
        delegate.applyDayNight()
    }
    getString(R.string.Light) -> {
        themeBackground = getString(R.string.Light) // setting a global variable with the current theme mode
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        delegate.applyDayNight()
    }
    getString(R.string.Dark) -> {
        themeBackground = getString(R.string.Dark)
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        delegate.applyDayNight()
    }
    getString(R.string.Amoled) -> {
        themeBackground = getString(R.string.Amoled)
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        delegate.applyDayNight()
        theme // overridden getTheme method
    }

the overridden get theme method used to set the black background

override fun getTheme(): Theme {
    val theme: Theme = super.getTheme()
    when (themeBackground) {
        getString(R.string.Light) -> theme.applyStyle(R.style.light, true)
        getString(R.string.Dark) -> theme.applyStyle(R.style.dark, true)
        getString(R.string.Amoled) -> theme.applyStyle(R.style.midnight, true)
    }
    return theme
}

I am able to change the theme but can't get the black background. Following are the styles which inherit the app theme.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="dialogThemeMenu" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:background">?attr/cardBackgroundColor</item>
    </style>

    <style name="midnight" parent="Theme.appTheme">
        <item name="backgroundColor">@color/black</item>
    </style>

    <style name="light" parent="Theme.appTheme">
        <item name="backgroundColor">@color/white</item>
    </style>

    <style name="dark" parent="Theme.appTheme">
        <item name="backgroundColor">@color/dark_background</item>
    </style>

</resources>
0

There are 0 best solutions below