I have a theme that user can choose light or dark, everything work fine but, there are 2 problems:
- When I open the app [in dark/light theme ], a white page is displayed first, then the application uses a dark theme
- When I change the theme, a black screen appears first
I tried to change the background but did not work, I changed the color of it and also I used drawable shape. Anyway, it not work and use one color for both.
style.xml
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"/>
values/themes.xml
<style name="Theme.MaterialComponents.DayNight.NoActionBar"
parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@drawable/window_background</item>
</style>
values-night/themes.xml
<style name="Theme.MaterialComponents.DayNight.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/TabBackground</item>
<item name="colorAccent">@color/green_inactive</item>
<item name="android:windowBackground">@drawable/window_background_dark</item>
</style>
windows_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#64b5f6"/>
</shape>
windows_background_dark
Similar to the above code, only the color is different
and this is how I load theme
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.getIsDarkMode(getApplicationContext()))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main);
}