I am learning how to implement Dark Theme to application.
After reading documents from this, below are the requirements that I understand
<style name="AppTheme" parent="Theme.AppCompat.DayNight">AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); and AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); inside onCheckedChanged listenerBased on testing, calling AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); will trigger recreate() to recreate the activity, result it will change the theme as expected, also the BLACK blinking thingy will appear.
After reading through this, it stated that I need to declare setTheme method before      setContentView(R.layout.activity_main); so it will solve the BLACK thingy issue.
The Android Dev document does not mentioned anything about setTheme is required in OnCreate method after AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); is used
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); able to switch desire theme, what's the point of using setTheme again in onCreate method?~~~~~~~~~~~~~~~~~~~~~~~~~~~ UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Maybe my questions is confusing as well. To make it short, just wanted to know that why "setTheme" is needed to declared in `onCreate` when `AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)` already restarted the acitivy?
I'm so confused and spent more than a week to solve this, guidance or tips are much appreciated
The AppCompactDelegate some times recreates the activity that causes black blinking
Check This It Changes theme without black blinking
Create two themes or use default dark theme xml like this :
<!--  Dark theme  -->
<style name="DarkTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/darkColorPrimary</item>
<item name="colorPrimaryDark">@color/darkColorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="fontFamily">@font/roboto</item>
<item name="listBackground">@color/darkListBackground</item>
<item name="cardBackground">@color/darkCardBackground</item>
<item name="textColor">@color/darkCardTitle</item>
<item name="actionBarBackgroundColor">@color/darkColorPrimary</item>
<item name="actionBarTextColor">@color/darkCardTitle</item>
<item name="dialogTextColor">@color/darkDialogTitle</item>
<item name="dialogDateColor">@color/dialogDateColor</item>
</style>
<!--  Light theme  -->
<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/lightColorPrimary</item>
<item name="colorPrimaryDark">@color/lightColorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="fontFamily">@font/roboto</item>
<item name="listBackground">@color/lightListBackground</item>
<item name="cardBackground">@color/lightCardBackground</item>
<item name="textColor">@color/lightCardTitle</item>
<item name="actionBarBackgroundColor">@color/lightColorPrimary</item>
<item name="dialogTextColor">@color/lightDialogTitle</item>
<item name="dialogDateColor">@color/dialogDateColor</item>
</style>
Use Shared Prefrence to save themes type
SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("NightMode", false);
        editor.apply();
Change App theme on Activity on create method using shared prefrence
 if (!darktheme) {
            setTheme(R.style.DarkTheme);
        } else {
            setTheme(R.style.LightTheme);
        }
For Brief Information get that project : https://github.com/yeshasmp/ToDo/tree/v1.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With