Reputation: 21
i added a splash screen to my application and it works.
But as i searched the internet, all guides and how-to explanations for android splash screen are doing it different.
Now i was wondering whether there are downsides to my solution ?
I do not have an extra splash activity, my solution is to just set a background for my main activity and the app gets displayed over the background once it is loaded. This has noticable less code to write to add a splash screen.
Note:
i got a solution for the whitescreen that gets displayed in between splash screen and app start, but this is not the focus of my question.
Here are the important files for the android splash.
Android styles.xml
<resources>
<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">
<item name="colorOnActionBar">?attr/colorOnSurface</item>
<item name="android:windowBackground">@drawable/splash_background</item>
<!-- These are the React default theme colors, if your theme is different, adjust accordingly -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorSecondary">@color/colorSecondary</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
AndroidManifest.xml
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Upvotes: 1
Views: 3656
Reputation: 634
I do something like this:
Step 1.
Create splash screen drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="@color/white"/>
<item android:drawable="@drawable/ic_app_icon"
android:gravity="center"
android:height="120dp"
android:width="120dp"/>
</layer-list>
Step 2.
Create style for your splash screen:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
<item name="actionOverflowMenuStyle">@style/Widget.MaterialComponents.PopupMenu.Overflow</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="android:windowBackground">@drawable/drawable_splash_screen</item>
<item name="android:navigationBarColor">#FFFFFF</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppTheme.FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="SplashTheme" parent="AppTheme.FullScreen">
<item name="android:windowBackground">@drawable/drawable_splash_screen</item>
<item name="android:navigationBarColor">#FFFFFF</item>
</style>
Step 3.
Do this in your actvity:
@Override
protected void onCreate(Bundle _savedInstanceState) {
super.onCreate(_savedInstanceState);
setContentView(R.layout.activity_main);
//Change window background from splash screen drawable to solid color.
getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
End you get beautiful, no time-wasting splash screen:
Upvotes: 0
Reputation: 708
Google Team has released New Splash Screen Api for Android 12 and below.
Reference:
Steps:
Set a theme for the splash screen to change its appearance
You can specify the following attributes in your Activity theme to customize the splash screen for your app. If you already have a legacy splash screen implementation that uses attributes like android:windowBackground, consider providing an alternate resource file for Android 12 and higher.
Use windowSplashScreenBackground to fill the background with a specific single color:
<item name="android:windowSplashScreenBackground">@color/...</item>
Use windowSplashScreenAnimatedIcon to replace an icon in the center of the starting window. If the object is animatable and drawable through AnimationDrawable and AnimatedVectorDrawable, you also need to set windowSplashScreenAnimationDuration to play the animation while showing the starting window.
<item name="android:windowSplashScreenAnimatedIcon">@drawable/...</item>
Use windowSplashScreenAnimationDuration to indicate the duration of the splash screen icon animation. Setting this won't have any effect on the actual time during which the splash screen is shown, but you can retrieve it when customizing the splash screen exit animation using SplashScreenView#getIconAnimationDuration. See Keep the splash screen for longer periods in the following section for further details.
<item name="android:windowSplashScreenAnimationDuration">1000</item>
Use windowSplashScreenIconBackgroundColor to set a background behind the splash screen icon. This is useful if there isn’t enough contrast between the window background and the icon.
<item name="android:windowSplashScreenIconBackgroundColor">@color/...</item>
Optionally, you can use windowSplashScreenBrandingImage to set an image to be shown at the bottom of the splash screen. The design guidelines recommend against using a branding image.
<item name="android:windowSplashScreenBrandingImage">@drawable/...</item>
Keep the splash screen on-screen for longer periods
The splash screen is dismissed as soon as your app draws its first frame. If you need to load a small amount of data such as in-app settings from a local disk asynchronously, you can use ViewTreeObserver.OnPreDrawListener to suspend the app to draw its first frame.
Upvotes: 1
Reputation: 118
Common Mistake
In most of the application developers use splash screen to showcase brand icon or picture for couple of seconds. This is common practice which most of the developers are following. It is not a good idea to use a splash screen that wastes a user’s time. This should be strictly avoided.
With the common approach you may also lead the problem of blank white page appears during splash launching.
Right Way
The right way of implementing a splash screen is a little different. In the new approach specify your splash screen’s background as the activity’s theme background.
Also the root cause of blank white page problem is that your layout file is visible only after app has been initialized completely.
Do not create a layout file for splash activity. Instead, specify activity’s theme background as splash layout.
Source medium.com
Upvotes: 1