Black Screen Before Splash Screen Appear in Android

blank screen comes before splash

Generally speaking, splash screens are not recommended for an app but if you really must.

Android will load a blank layout before it loads an activity layout based on the theme you have set for it. The solution is to set the theme of the splash activity to a transparent one.

Create a transparent theme in res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>

Then set the theme in your manifest

<activity android:name=".SplashActivity" android:theme="@style/Theme.Transparent">
...
</activity>

Black screen before Splash screen appear in android

Add a theme with the background you are using to your application tag in the manifest file to prevent the black screen to be drawn.

theme.xml

<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:style/Theme" />

<style name="Theme.MyAppTheme" parent="Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/my_app_background</item>

</style>
</resources>

AndroidManifest.xml

....
<application
android:name="@string/app_name"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.MyAppTheme"
>
....

Read why there is a black screen here On app launch, Android displays a simple preview window (based on your activity theme) as an immediate response to the user action. Then the preview window crossfades with your actual UI, once that has fully loaded. To ensure a smooth visual transition, your activity theme should match your full UI as closely as possible. The below image shows how the experience can be jarring if not handled properly.

Black screen after splash

I found a solution after a while:

class SplashActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Timer().schedule(1000) {
if (sharedPrefs.isUserLogged) {
startActivity(intentFor<MainActivity>().clearTask().newTask())
}else{
startActivity(intentFor<LoginActivity>().clearTask().newTask())
}
}
}
}

and I set the theme like this:

<style name="AppTheme.Launch">
<item name="android:windowBackground">@drawable/my_splash</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

How can I remove white screen which appear before splash screen?

Finally got my answer Splash Screen in Right Way. I do just following.

In values->styles.xml I created splash screen background image

<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>

For below api 19, in values-19->styles.xml I used

<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>

I removed setContentview() from SplashActivity and added style for splash screen in Manifest.xml file android:theme="@style/AppTheme.Splash"



Related Topics



Leave a reply



Submit