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.
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".
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.
Basically I just want to know about night-mode and if I can use it for the splash screen:
When do the "night mode" that Android supports for so long kick in?
Is it a os-global mode? Does the user control it?
Is it automatic? Or completely manual by the current app?
Does the mode stay later, even if I set it manually by the app and the app is killed ?
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?
Is my code correct for this?
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.
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.
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" ...">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With