android developer
android developer

Reputation: 116332

Is it possible to have a night-qualifier for splash screen on Android?

Background

I know there is a new feature on Android Q to finally support dark theme (wrote here about detecting it).

I also know that there is "Night Light" feature (here) that makes the screen become more yellow-ish.

Supporting the theme choosing manually is something I've done for years (using simply setTheme on Activity's onCreate's first line of code), but I want to know if there is something automatic that will allow me to set it even before the app really starts, in the splash screen.

The problem

It seems a very old feature (since API 8!) has existed on Android for a very long time, of having "night" qualifier in the resources, and I never even tried it.

Sadly, because "dark theme" and night-related stuff are now more often talked about as the new features (here for example), I can't find what's the old one all about.

Looking at some articles and documentations, though, it seems it's almost completely manual:

What I've tried

I tried to set the various themes accordingly:

res/drawable/splash.xml

  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
      <shape android:shape="rectangle">
        <solid android:color="#fff"/>
      </shape>
    </item>
    ...
  </layer-list>

res/drawable-v29/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
  <item android:gravity="fill">
    <shape android:shape="rectangle">
      <solid android:color="?attr/colorSurface"/>
    </shape>
  </item>
  ...
</layer-list>

res/drawable-night/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:opacity="opaque" tools:ignore="UnusedAttribute">
  <item android:gravity="fill">
    <shape android:shape="rectangle">
      <solid android:color="#000"/>
    </shape>
  </item>
  ...
</layer-list>

manifest

res/values/themes.xml

  <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
    <item name="android:windowBackground">@drawable/splash</item>
  </style>

res/values-v26/themes.xml

  <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
    <item name="android:windowSplashscreenContent">@drawable/splash</item>
  </style>

I tried to remove the android:windowBackground and android:windowSplashscreenContent, trying to see if it's automatic , but it didn't help.

The questions

Basically I just want to know about night-mode and if I can use it for the splash screen:

  1. When do the "night mode" that Android supports for so long kick in?

  2. Is it a os-global mode? Does the user control it?

  3. Is it automatic? Or completely manual by the current app?

  4. Does the mode stay later, even if I set it manually by the app and the app is killed ?

  5. Is it possible to set the theme of the application (meaning for the splash screen) to use this, so that while night-mode is enabled, it will use it to have a dark background?

  6. Is my code correct for this?

Upvotes: 7

Views: 3036

Answers (1)

android developer
android developer

Reputation: 116332

Actually it is possible to have a night modifier for splash screen.

Sadly, it happens right away, before any code can be used . So it's completely based on the settings of the OS, and not of the app itself.

Also, there is an annoying issue on the IDE, that for each Activity it will first use this theme that I've created for the splash.

Other than those 2 disadvantages, works fine, and you can see it for yourself on my app here.

Here:

res/drawable/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#fff" />
        </shape>
    </item>
    <item
        android:width="48dp" android:height="48dp" android:gravity="center">
        <bitmap
            android:gravity="center" android:src="@mipmap/ic_launcher_foreground" />
    </item>
</layer-list>

res/drawable-night/splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:opacity="opaque" tools:ignore="UnusedAttribute">
    <item android:gravity="fill">
        <shape android:shape="rectangle">
            <solid android:color="#000" />
        </shape>
    </item>
    <item
        android:width="48dp" android:height="48dp" android:gravity="center">
        <bitmap
            android:gravity="center" android:src="@mipmap/ic_launcher_foreground" />
    </item>
</layer-list>

themes.xml

    <style name="AppTheme_splash" parent="@style/Theme.MaterialComponents.DayNight">
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:navigationBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
        <item name="android:statusBarColor" tools:targetApi="lollipop">?android:colorBackground</item>
        <item name="colorSecondary">?colorAccent</item>
        <item name="android:colorSecondary" tools:targetApi="n_mr1">?colorAccent</item>
    </style>

manifest

  <application android:theme="@style/AppTheme_splash" ...">

Upvotes: 2

Related Questions