Let’s start with some suggestions about how a splash screen should look:
It shouldn’t be too complicated.
No important info should be shown there.
Remember that it is just a loading screen.
When it comes to the implementation we can split adding a splash screen to an app into two main steps:
Add a native splash screen to each platform - as you would do in a native app.
Adjust the splash screen implementation to react-native.
Why can’t we just add a splash screen natively? Because there is a time gap before a splash screen disappears on the native side and the first screen rendering on React Native side takes place, which causes a white blink.
In this tutorial, I will show how to create a basic splash screen containing a logo and a custom background color.
IOS:
1. Prepare your splash screen by editing LaunchScreen.xib. If you are not familiar with Xcode, the Adding Splash Screen IOS section from this blogpost will be helpful.
2. Install react-native-splash-screen:
yarn add react-native-splash-screen
or
npm install react-native-splash-screen --save
If you use React Native version 0.60 you don’t have to link the library. Otherwise, run:
react-native link react-native-splash-screen.
3. If you are using cocoapods (default in React Native 0.60+), you have to also run the pod install command from the project's iOS folder.
4. Edit your AppDelegate.m file to make your splash screen displays while the JS code is loading. Open AppDelegate.m and add following code:
5. Optional: If you have a dark background, you probably will want to change the status bar icons’ color. You can do that by adding these two lines to Info.plist:
The available options here are: UIStatusBarStyleLightContent and UIStatusBarStyleDarkContent. Remember that making a change here will affect your entire app. To control the status bar within a React Native app, use the StatusBar component.
6. The last thing to do is hide the splash screen when the first screen is mounted. But remember, you can hide the splash screen at any moment, e.g when the initial data is fetched.
1. Create an activity which is responsible for forwarding to the main activity, which is the React Native app. In app/src/main/java/[packageName] create a SplashActvity.java file with the following code:
package com.splashscreen; // ← Make sure that is your package nameimport android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; // ← For RN <= 0.59import android.support.v7.app.AppCompatActivity; // ← For RN >= 0.60publicclassSplashActivityextendsAppCompatActivity{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
2. Modify your AndroidManifestFile.xml to use SplashActivity initially:
Now, similar to iOS, we need to make the splash screen display until the first component is mounted.
6. Firstly, install the react-native-splash-screen library if you haven’t installed it already. If you use React Native > 0.60, the package will be linked automatically. Otherwise, you need to run the linking command:
react-native link react-native-splash-screen
7. Then modify MainActivity.java:
package com.splashscreen;
+++ import org.devio.rn.splashscreen.SplashScreen;
+++ import android.os.Bundle;
import com.facebook.react.ReactActivity;
publicclassMainActivityextendsReactActivity{
+++ @Override
+++ protectedvoidonCreate(Bundle savedInstanceState){
+++ SplashScreen.show(this, R.style.SplashStatusBarTheme);
+++ super.onCreate(savedInstanceState);
+++ }
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/@Overrideprotected String getMainComponentName(){
return"splashScreen";
}
}
8. As you can see, a custom theme is passed as the second parameter to the show method. Modify style.xml once again by adding a new theme.
<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><itemname="android:textColor">#000000</item><itemname="android:statusBarColor">@color/background</item></style><stylename="SplashTheme"parent="Theme.AppCompat.Light.NoActionBar"><itemname="android:background">@drawable/background_splash</item><itemname="android:statusBarColor">@color/background</item></style>
+++ <stylename="SplashStatusBarTheme"parent="SplashScreen_SplashTheme">
+++ <itemname="android:statusBarColor">@color/background</item>
+++ </style></resources>
It’s recommended to do that. Otherwise, StatusBar will change color to black as the app's JS code loads.
9. To work properly the react-native-splash-screen library needs a file named launch_screen.xml inside the app/src/main/res/layout directory. In this file, create a layout using the previously created background.
10. The last thing to do is adding a color called primary_dark in app/src/main/res/values/colors.xml. We don’t use it directly in code, but without that resource, the app will crash.