How to add a dark mode splash screen to a React Native app
Welcome to the dark side! Dark mode is everywhere after the new iOS 13 update. A lot of developers have started adjusting apps to support a dark theme but a lot of developers are also forgetting to adjust the splash screen. In this tutorial I will show you how to give your app a dark mode splash screen in 10 minutes.
You can toggle a light or dark splash screen based on your phone’s settings. In Android, you can find the setting here: Settings -> Display -> Dark mode
. In iOS: Settings -> Display -> Brightness
.
iOS Dark Mode Splash Screen
Let’s do iOS first. Open your react-native iOS project in Xcode.
Add your image assets to Images.xcassets and on the right-side options section set the Appearance to Any, Light, Dark
. Fill the image gaps as needed. In my example, the smiling face is for light mode, and the sad face is for dark mode.
In the LaunchScreen.xib, set the background for the view as a System Background Color
. You can do the same thing for the image as well.
Easy, yes?
Android Dark Mode Splash Screen
Let’s address Android. Open your project in Android Studio. In my sample Android project I have SplashScreenActivity which basically starts MainActivity.
In the AndroidManifest.xml file you need to define SplashScreenActivity in LAUNCHER
mode. I also added android:theme=”@style/SplashTheme”
. This line is responsible for our splash theme.
The next step is to add a directory for values-night
. The file structure for res
should be similar to the following:
In the values-night
folder, create a styles.xml
file with the following:
I added a background to SplashTheme as a drawable with my splash screen layout for dark mode. Do the same for light mode, as follows:
Now you need to add two drawables: splash_screen_dark.xml
and splash_screen_light.xml
, and set the images as needed. Here is my example:
Rebuild the project and that’s it :)
If you’re using react-native-splash-screen
, you can set the theme colors for your launch_screen
in MainActivity.java
.
You also need to update styles.xml
. Similar to the following:
Create a theme.xml
file inside the values
folder and add the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="textColor" format="color"/>
<attr name="colorAccent" format="color"/>
<attr name="backgroundColor" format="color"/>
<attr name="splashBackgroundColor" format="color" />
</resources>
After that, you can refer to values:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backgroundColor"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/logo"
/>
</RelativeLayout>
Summary
Adding a splash screen based on a user’s theme settings is super easy for iOS. For Android, like most of the time, it’s a bit more effort to make it work but not that difficult. I hope you enjoy my tutorial and if you like it you can give me a 👏. May the force be with you! 💫