Get rid of white screen at startup

Viewed 1331

I've been searching online for the right way to avoid the white screen - replacing it with a splashscreen - when my Android app launches. I found solutions that uses styles like

<style name="AppTheme.SplashTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

But in my case this only delays the view appearance, acting as the app doesn't work (the view inflates after a couple of seconds). I do not want some view that lasts for some seconds I've decided, I just want to replace the blank screen to something custom while my app needs to load. The only way to have the desired behaviour was to place a drawable as a background in the style of my SplashscreenActivity.

<item name="android:windowBackground">@drawable/background_splash</item>

Now, I would like not to place an image as a background-because of the different resolutions of screens- but to set an xml layout. Is this possible? What's the best way to get rid of the blank screen without delaying the view appearance?

1 Answers

You should do it as follows

  • create an xml file in res/drawable folder with below content. This will act as your splash screen. Let's name it splash.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:drawable="@color/colorPrimary" />
       <item>
            <bitmap android:src="@drawable/splash_logo"
        android:gravity="center" />
      </item>
    </layer-list>
    
  • Then you need to specify a style/theme without titlebar for your splash view, so add below style to your style.xml file.

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash</item>
    </style>
    
  • In AndroidManifest.xml change theme of your launcher activity to use SplashTheme

        <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
  • Finally in that launcher activity (in my example MainActivity) change the theme back to default application theme, usually named AppTheme

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState);    
        ...
    }
    

That's it! Now run your application and see that there is no white screen anymore.

References: I used this post and this one to provide the above code example.

Related