2

In the development of the app I am encountering this problem which has already been discussed in numerous other posts but which have not given me any solution. In particular I am implementing a custom splash screen. It's custom because I don't want to show a static image as a splash but an animation.

In particular, I'm using Lottie as a library for animations, and therefore the "classic" way of creating a Theme in the Style and calling it in the OnCreate method before the super etc. etc. I don't need it, I want to go directly into the SpashActivity.

The problem is that however I can't start the app without the two seconds of white screen. I don't want to change color I just want to avoid it and go directly to the Splash activity. Anyone have any suggestions? I have already tried several ways, I put some of them below:

Things I've tried but that DON'T work:

<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>

adding the following stuff inside AndroidXml

android:theme="@android:style/Theme.Translucent.NoTitleBar"

this way freeze my screen:

<item name="android:windowDisablePreview">true</item>
DoctorWho
  • 1,044
  • 11
  • 34
  • Can't believe your question doesn't have more upvotes considering how many search results asking this question showed up when I looked. Hopefully my answer clears some things up for anyone seeing this question. – LabGecko Aug 09 '23 at 14:23

6 Answers6

1

Use drawable image

 <style name="FullscreenTheme" parent="AppTheme">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

And in menifest

 <activity
        android:name=".SplashActivity"
        android:theme="@style/FullscreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
hardkgosai
  • 309
  • 3
  • 5
  • How that can help me? – DoctorWho Nov 25 '20 at 14:24
  • 1
    Hi. Please add some explanation to your answer instead of just posting code. It will help others to understand better. – WhiteSpidy. Nov 25 '20 at 15:09
  • @DoctorWho i updated my answer please try it. it will works. – hardkgosai Nov 25 '20 at 15:59
  • 1
    That's not the way to avoid white screen. This is how made a classical splash. did you check the link i put in the post i wrote? I don't need to make a splash, I just want to remove the white screen by opening the app – DoctorWho Nov 25 '20 at 16:05
  • 2
    It's Impossible. System take time in ms to start code and that's why white screen appear and for this ms time you can show your drawable instead of white screen – hardkgosai Nov 25 '20 at 16:12
1

I ran into this same issue. Currently, the only way to reduce this cold start screen time is to reduce the number of things that first screen is loading or to load them while other things are showing (asynchronously). That is outside the scope of this answer, so the workaround is to show the app logo or some image while it loads. These steps should accomplish that.

1 - Make a background

If you don't have a black background object, create a file named background_black.xml in the /res/drawable folder. Put this code in it:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@android:color/black" />
    </shape>

Optional: You could also use an image, but it must be a non-animated bitmap. To do this, INSTEAD of the code above, use something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Base background color -->
        <item>
            <color android:color="@color/black"/>
        </item>
    
        <!-- Logo centered on the screen -->
        <item>
            <bitmap
                android:src="@drawable/logo"
                android:gravity="center" />
        </item>
    </layer-list>

Ideally, to avoid code confusion the image background code (above) would be in a file named something like background_splash_screen.xml and you would have to use that name anywhere background_black is used below. Using this method you would also need to import your logo or image into /res/drawable, then either name it logo or change logo in the XML above to the name of your image.

A good idea is also to be sure you have versions of the image in sizes that match what your users are likely to see, but importing using Android Studio should take care of that for you. I tested importing on Giraffe (version 2022.3.1) of Android Studio. On that version the option was in File >> New >> Image Asset.

To do this manually, put the correctly-sized version of the image in the /res folders drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi, and drawable-xxxhdpi.

2 - Add the background to your Theme

In res/values/themes.xml make sure you have

    <style name="AppTheme" parent="Theme.Material3.DayNight">
        <!-- Other theme attributes -->
        <item name="android:windowBackground">@drawable/background_black</item>
    </style>

Make sure you change AppTheme to the name of the theme you are using. Also, make sure the parent matches the theme in your AndroidManifest.xml (more below).

An important caution from the docs:

If you're styling your app and not seeing the results you expect, it's likely that other styling is overriding your changes.

If you have multiple themes you can use theme inheritance to make them work together.

3 - Make sure your theme is in the Android Manifest

In AndroidManifest.xml either set the global app theme

<application
    android:theme="@style/AppTheme"
    >
</application>

or set the theme for your startup screen

<activity
    android:name=".YourLaunchActivity"
    android:theme="@style/AppTheme"
    >
</activity>

Of course, for either option make sure you add any other settings you may need. For example, for application you might also need android:icon="@mipmap/ic_launcher" and android:label="@string/app_name". Or for the activity you might need a label like android:label="@string/title_app_activity" to help with localization. Of course you would need to set the text for both app_name and title_app_activity in your strings.xml file (currently by default in the /res/values/ folder).

'Workarounds' NOT to use

Do not use windowDisablePreview. It increases start time and prevents interaction with no feedback to the user. From the docs:

You may see extra time added during startup if you have previously used one of the following methods to implement a custom splash screen in Android 11 (API level 30) or lower:

Using the windowDisablePreview theme attribute to turn off the initial blank screen drawn by the system during launch. Using a dedicated Activity. Starting with Android 12, migrating to the SplashScreen API is required. This API enables a faster startup time, and also allows you to tweak your splash screen

Do not use

<item name="android:windowIsTranslucent">true</item>

Based on answers to other posts about this question, it can crash the app or make objects untargetable.

Do not use

android:theme="@android:style/Theme.Translucent.NoTitleBar"

This can cause unexpected issues.

These code snippets could be useful in other situations, but for fixing this cold start screen they often cause problems.

LabGecko
  • 193
  • 10
0

This happens because of ‘Cold Start’. ‘Cold Start’ is the time taken from the moment the code is initialized till the UI is responsive to the user. It happens in cases such as your app is being launched for the first time since the device booted, or since the system killed the app.During this time white blank screen comes up. Android OS first checks Manifest is there any theme for the activity and loads the theme from manifest if present, else loads UI of the activity.

How to get rid of this white Blank screen?

Add theme for the splash activity in manifest.

Here is it explained in good way, follow the same blog

https://medium.com/@shishirthedev/the-right-way-to-implement-a-splash-screen-in-android-acae0e52949a

  • If u follow the link in my post u can manage a splash screen in a better way. So, I need to avoid it not replace with old way to do a spash. Sorry but don't help me – DoctorWho Nov 25 '20 at 14:26
0

Try adding this to AppTheme in styles.xml

 <item name="android:windowBackground">@android:color/transparent</item>
 <item name="android:colorBackgroundCacheHint">@null</item>

Here's the full code to styles.xml (themes.xml)

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>
</resources>
Vishnu
  • 663
  • 3
  • 7
  • 24
0

Here's styles.xml

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

Here's splash_screen.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" />
<item
    android:width="@dimen/_150sdp"
    android:height="@dimen/_150sdp"
    android:bottom="24dp"
    android:drawable="@drawable/app_logo"
    android:end="24dp"
    android:gravity="center"
    android:start="24dp"
    android:top="24dp" />

Give theme to your launcher activity

        <activity
        android:name=".activities.SplashActivity"
        android:screenOrientation="portrait"
        android:exported="true"
        android:theme="@style/SplashTheme">

Note : Remove setContentView(R.layout.activity_splash); from your lanucher activity

Tombstone
  • 208
  • 2
  • 5
-1

All you need is drawable with layer-list and theme with <item name="android:windowBackground">@drawable/your_drawable</item> and don't use setContentView to keep windowBackground.

Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46
  • No. I don't need it. Because I don't want to use a static splash screen. I need to use Lottie, I need to use animation with it and avoid only white screen, cold start or whatever name this boot has – DoctorWho Nov 25 '20 at 16:03
  • 1
    @DoctorWho it's impossible to use animation for cold start – Stanislav Bondar Nov 25 '20 at 16:08
  • exactly. and it is precisely for this reason that I would like to understand how to avoid making these two seconds of white screen appear and then switch to my activity, where it is possible to put any animation – DoctorWho Nov 25 '20 at 16:10