androidandroid-themeandroid-night-mode

Android Change branded launch background colors when theme is dark


I've implemented dark theme in my application. Everything works fine, when system is on dark mode the application is on dark mode and vice versa.

The problem is Branded Launch.I have 2 drawable files for it:

Dark:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

    <item android:drawable="@color/splashColorDark"/>

    <item>

        <bitmap
                android:src="@drawable/logo_branded"
                android:mipMap="true"
                android:gravity="center"/>

    </item>

</layer-list>

And light:

 <?xml version="1.0" encoding="utf-8"?>

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

        <item android:drawable="@color/splashColorLight"/>

        <item>

            <bitmap
                    android:src="@drawable/logo_branded"
                    android:mipMap="true"
                    android:gravity="center"/>

        </item>

    </layer-list>

In order to use branded launch I added this:

android:theme="@style/AppTheme.BrandedLaunchLight"

in my AndroidManifest.xml

Doing this obviously I'm not able to set dark branded launch when dark mode is active. What should I do?


Solution

  • Just use a DayNight theme (Theme.MaterialComponents.DayNight or Theme.AppCompat.DayNight) in your app.
    Then use the -night qualifier on your resource folders as drawable-night and values-night.

    It means use the same name for your drawable in the branded launch for dark and light theme but use the drawable-night and drawable folder.
    You can do the same for the colors. Instead of using @color/splashColorDark and @color/splashColorLight use a single name splashColor and put it in the values-night\colors.xml and values\colors.xml

    Note. Check the official documentation:

    Launch screens

    If your app has a custom launch screen, it may need to be modified so that it reflects the selected theme.

    Remove any hardcoded colors, for example any background colors pointing may be white. Use the ?android:attr/colorBackground theme attribute instead.

    Note that dark-themed android:windowBackground drawables only work on Android Q.