Search code examples
androidandroid-themeandroid-styles

Change background color in launch screen when using DayNight theme


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>


Solution

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