Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • https://developer.android.com/reference/android/app/UiModeManager.html - says I can set it myself, including set it to be automatic. However, it seems it's related to being docked, and maybe even car-mode.

  • https://google-developer-training.github.io/android-developer-fundamentals-course-concepts/en/Unit%202/53_c_providing_resources_for_adaptive_layouts.html - Says I can set "night" as a qualifier, and there is a "night mode".

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?

like image 425
android developer Avatar asked Jun 29 '19 19:06

android developer


People also ask

Does Android have a splash screen?

Starting in Android 12, the launch animation for all apps when running on a device with Android 12 or higher. This includes an into-app motion at launch, a splash screen showing your app icon, and a transition to your app itself.

How do you animate a splash screen on Android?

Go to app > java > first package name > right-click > New > Activity > Empty Activity and create another activity and named it as SplashScreen. Edit the activity_splash_screen. xml file and add image, text in the splash screen as per the requirement. Here we are adding an image to the splash screen.


1 Answers

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" ...">
like image 79
android developer Avatar answered Sep 21 '22 22:09

android developer