Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to Implement Dark Theme Android Java

I am learning how to implement Dark Theme to application.
After reading documents from this, below are the requirements that I understand

  1. make sure "Style" parent = <style name="AppTheme" parent="Theme.AppCompat.DayNight">
  2. create a new colors for Dark Theme in side colors.xml file
  3. if using "Switch" toggle, use AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); and AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); inside onCheckedChanged listener

Based 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

  1. If using only AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); able to switch desire theme, what's the point of using setTheme again in onCreate method?
  2. Is the BLACK blinking thing a bug? or is there a proper way to solve it?

~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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

like image 621
Arduino Avatar asked Oct 29 '25 07:10

Arduino


1 Answers

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

like image 137
Gourav Gautam Avatar answered Oct 31 '25 06:10

Gourav Gautam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!