29
votes

I am using DayNight theme with launch screen.

Launch screen is a layer-list with white background.

So when its day, white launch screen shows followed by white activity. But at night, white launch screen shows follwed by dark activity.

How can i change the background color in the launch screen according to the theme.

Cannot use custom color attrs because there is only a DayNight theme.

themes.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorControlNormal">@color/colorAccent</item>
</style>

<style name="LaunchTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/launch_screen</item>
</style>

launch_screen.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <color android:color="@color/material_white"/>
</item>
<item>
    <bitmap
       android:gravity="center"
       android:src="@drawable/launch_logo"
       android:tileMode="disabled"/>
</item>

2
Read this you will get some Idea : blog.nkdroidsolutions.com/…Harshad Pansuriya
Thanks a lot for the link. Didn't knew about drawable-night and values-night folders. This will do :)Amit Jayant
Well unfortunately, night qualifiers does not work with launch screens. Because launch screen is loaded by OS, not the app. :(Amit Jayant
Yes, but If you change android:windowBackground in styles it is succeeds changing splash background. The problem is that DayNight mode is not changed until startup. Since it's a dynamic thing (Can be changed based on Preference).Ioane Sharvadze
Did you find any solution?Eren Tüfekçi

2 Answers

11
votes

In addition to your default launch theme provide its night variant in appropriate resource directory.

res/values-night/themes.xml

<style name="LaunchTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/launch_screen_night</item>
</style>

res/drawable/launch_screen_night.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <color android:color="@color/material_black"/>
</item>
<item>
    <bitmap
       android:gravity="center"
       android:src="@drawable/launch_logo"
       android:tileMode="disabled"/>
</item>
5
votes

After checking for a while, I found answer.

Since splash screen configuration is loaded by system, you will need to affect system UI mode.

getSystemService<UiModeManager>()?.nightMode = UiModeManager.MODE_NIGHT_YES

But unfortunately this is not clean solution, since it affects system settings.

Changes to night mode take effect globally and will result in a configuration change (and potentially an Activity lifecycle event) being applied to all running apps. Developers interested in an app-local implementation of night mode should consider using AppCompatDelegate.setDefaultNightMode(int) to manage the -night qualifier locally.