This lesson works fine here - I quoted it and changed it a bit, added a touch on react-native-background-color
.
https://medium.com/react-native-development/change-splash-screen-in-react-native-android-app-74e6622d699 (which is based on this https://www.bignerdranch.com/blog/splash- screens-the-right-way / - so this is a native Android technique)
You need to create a splash screen in res / drawable. Let's call it splash_screen.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/white"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list>
Now you need to update res / values /styles.xml
<resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> </style> <style name="SplashTheme" parent="AppTheme"> <item name="android:windowBackground">@drawable/splash_screen</item> </style> </resources>
Now open AndroidManifest.xml and replace AppTheme
with SplashTheme
in MainActivity
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/SplashTheme" android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
We use SplashTheme instead of updating AppTheme to add this background only for the beginning of activity and to leave other elements without changes.
After you try the above, you will notice that the splash screen will always remain under your js views. You have two options for hiding the screensaver:
- To set the background color that will delete the image, use the Reaction-native-background-color module from https://github.com/ramilushev/react-native-background-color . (This is the recommended method, because when the keyboard is displayed in some cases, it makes the root view visible for a split second.)
- Or set a solid (opaque) background color in the root view.
Pay attention to what the "root view" means. This is the very first <View>
that you render in your application that you control (this means that you can style it).
Custom color
If you want to use a custom color other than @android:color/***
, then you need to create colors.xml
in android\app\src\main\res\values\colors.xml
and define colors like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="foobar">#ca949b</color> </resources>
You can use any name and any color code.
Then in splash_screen.xml
we update <item android:drawable="@android:color/white" />
to <item android:drawable="@color/foobar" />
Additional background removal information for the rear screen saver
Once you have created a surge like this. You will notice that the image remains forever in the background. To remove this image, use this module - https://github.com/ramilushev/react-native-background-color - and call BackgroundColor.setColor('#XXXXXX')
any color. This will delete the image.
Instead of using the opaque color in the root view to cover the splash, it is still recommended to use the above module, because when the keyboard shows, the background view is displayed for a split second.
source share