119

I have an android app which displays a white screen for 2 seconds on startup. My other apps don't do this, but this one does. I have also implemented a splashscreen with the hope that it would fix this. Should I increase my splash screen sleep time? Thanks.

buczek
  • 1,989
  • 7
  • 28
  • 39
Anonymous
  • 1,349
  • 3
  • 11
  • 16

18 Answers18

170

Put this in a custom style and it solves all the problems. Using the hacky translucent fix will make your task bar and nav bar translucent and make the splashscreen or main screen look like spaghetti.

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

ireq
  • 1,733
  • 1
  • 9
  • 2
112

Just mention the transparent theme to the starting activity in the AndroidManifest.xml file.

Like:

<activity
        android:name="first Activity Name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

and extend that screen with Activity class in place of AppCompatActivity.

like :

public class SplashScreenActivity extends Activity{

  ----YOUR CODE GOES HERE----
}
Sachin
  • 4,110
  • 2
  • 16
  • 27
user543
  • 3,607
  • 2
  • 15
  • 14
  • please have a look on my question above – Anonymous Dec 13 '13 at 06:29
  • 4
    but using `final ActionBar actionBar = getActionBar();` will return null when the Theme is translucent – Someone Somewhere Apr 26 '14 at 07:51
  • @SomeoneSomewhere u got solution? – PankajAndroid Jun 12 '14 at 09:50
  • 19
    Pankaj - yes, I learned that the bad UI experience was due to the "preview" window... apparently someone at Google decided that a white preview window was really awesome to use by default. However, in an app that uses a dark background it's a really bad idea. The solution is to create a custom style for your app and specify 'android:windowBackground' to the color you want to use. See the section "the perfect preview window" http://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/ – Someone Somewhere Jun 20 '14 at 15:26
  • Thanks buddy i was in a hunt for Theme.Translucent.NoTitleBar :) – manukv Feb 03 '16 at 10:53
  • 2
    @Hagai L It's give me an error like as "java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity." – TejaDroid May 27 '16 at 09:46
  • 1
    android:theme="@android:style/Theme.Translucent.NoTitleBar" did not work for me, any suggestions? – jay Aug 01 '16 at 08:01
  • I set the android:theme on application tag. Is that different from setting it on Activity tag in the manifest? – Kala J Aug 11 '16 at 11:37
  • 1
    @TejaDroid to get stop error "java.lang.IllegalStateException" you have to extends your Activity by android.app.Activity, right now you are using AppCompactActivity. So for AppCompactActivity you cannot use normal themes. – Ready Android Jan 11 '18 at 05:14
  • When I try with Theme.Translucent.NoTitleBar then app is taking time in lauching. It seems like first launch activity is drawing the translucent theme to overcome the white screen issue and then its draw the content view. Any idea about this? How to overcome this delay issue, I checked with Play Store app also that is also taking time to launch but very less approx 300ms but mine is taking 700ms to 1s. – Ready Android Jan 11 '18 at 05:17
79

enter image description here

Like you tube.. initially they show icon screen instead of white screen. And after 2 seconds shows home screen.

first create an XML drawable in res/drawable.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/gray"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Next, you will set this as your splash activity’s background in the theme. Navigate to your styles.xml file and add a new theme for your splash activity

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

</resources>

In your new SplashTheme, set the window background attribute to your XML drawable. Configure this as your splash activity’s theme in your AndroidManifest.xml:

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

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This link gives what you want. step by step procedure. https://www.bignerdranch.com/blog/splash-screens-the-right-way/

UPDATE:

The layer-list can be even simpler like this (which also accepts vector drawables for the centered logo, unlike the <bitmap> tag):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background color -->
    <item android:drawable="@color/gray"/>

    <!-- Logo at the center of the screen -->
    <item
        android:drawable="@mipmap/ic_launcher"
        android:gravity="center"/>
</layer-list>
Franco
  • 2,584
  • 2
  • 19
  • 27
Abhi
  • 3,012
  • 21
  • 14
  • 1
    This is really great, but how can I add text under the `mipmap/ic_launcher` – Hasan A Yousef Feb 10 '17 at 15:29
  • 6
    This is the best answer. Reason is that when using the above solutions, there is a delay in the visual startup of the app. – Jnr Feb 28 '17 at 07:58
  • one more fix to control the bitmap https://stackoverflow.com/questions/23079355/android-bitmap-image-size-in-xml – amorenew Oct 19 '17 at 07:23
  • 1
    This should be the accepted answer, all the others just make it look like the app never started from the user point of view. A simpler drawable `layer-list` can be this: ` ` – Franco Nov 07 '17 at 22:37
  • This solution doesn't solve the problem when we want to change background color in layer-list programmatically (in your example from gray e.g to blue). In this scenario, first gray screen appears for a second or two, then blue background is set! – Mohammad Afrashteh Jan 20 '18 at 15:42
  • Definitely the best solution on this page. It is easy to implement and it doesn't slow down the booting process. – Hawklike Mar 24 '20 at 21:42
  • This solution is great but I still have a white screen between the splashScreen and the main activity. Is it possible to edit the minimum duration of the splashScreen so it will be visible while the main activity is loading? – Nicolaesse Dec 11 '21 at 12:20
72

Make a style in you style.xml as follows :

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <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>

and use it with your activity in AndroidManifest as:

<activity android:name=".ActivitySplash" android:theme="@style/Theme.Transparent">
Tarun Deep Attri
  • 7,216
  • 8
  • 34
  • 55
8

You should read this great post by Cyril Mottier: Android App launching made gorgeous

You need to customise your Theme in style.xml and avoid to customise in your onCreate as ActionBar.setIcon/setTitle/etc.

See also the Documentation on Performance Tips by Google.

Use Trace View and Hierarchy Viewer to see the time to display your Views: Android Performance Optimization / Performance Tuning On Android

Use AsyncTask to display some views.

Blo
  • 11,775
  • 5
  • 43
  • 95
7

This is my AppTheme on an example app:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

As you can see, I have the default colors and then I added the android:windowIsTranslucent and set it to true.

As far as I know as an Android Developer, this is the only thing you need to set in order to hide the white screen on the start of the application.

luiscosta
  • 857
  • 1
  • 10
  • 16
4

Both properties works use any one of them.

    <style name="AppBaseThemeDark" parent="@style/Theme.AppCompat">
            <!--your other properties -->
            <!--<item name="android:windowDisablePreview">true</item>-->
            <item name="android:windowBackground">@null</item>
            <!--your other properties -->
    </style>
Sohail Zahid
  • 8,098
  • 2
  • 23
  • 39
4

The user543 answer is perfect

<activity
        android:name="first Activity Name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
 <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

But:

You'r LAUNCHER Activity must extands Activity, not AppCompatActivity as it came by default!

Community
  • 1
  • 1
Igor Fridman
  • 1,209
  • 1
  • 13
  • 27
  • i tried this solution many times as everywhere wrote this same solution, it couldn't work -- > but your BUT save my time my Activity extends AppCompatActivity that's why it's not working i changed it to activity now it works thank you man!! – Rucha Bhatt Joshi Mar 03 '17 at 05:47
4

The white background is coming from the Apptheme.You can show something useful like your application logo instead of white screen.it can be done using custom theme.in your app Theme just add

android:windowBackground=""

attribute. The attribute value may be a image or layered list or any color.

Rajesh.k
  • 2,137
  • 1
  • 15
  • 18
2

Below is the link that suggests how to design Splash screen. To avoid white/black background we need to define a theme with splash background and set that theme to splash in manifest file.

https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154

splash_background.xml inside res/drawable folder

<?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:gravity=”center”
 android:src=”@mipmap/ic_launcher” />
 </item>

</layer-list>

Add below styles

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

In Manifest set theme as shown below

<activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    </activity>
Rocky
  • 517
  • 4
  • 5
2

i also had the same problem in one of my project. I resolved it by adding some following parameters in the theme provided to the splash screen.

<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>

You can find the reason and resolution in this blog post written by me. Hope it helps.

divaPrajapati09
  • 160
  • 1
  • 12
  • Please add the details directly to your answer. Links can become invalid, that's why we like for the answer to contain the actual content, the link being only supplementary. – O.O.Balance Jun 24 '18 at 08:52
1

It can be fixed by setting the theme in your manifest as

<activity
        android:name=".MySplashActivityName"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
 <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

and after that if you are getting
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
then you may need to extend Activity instead of AppCompatActivity in your MySplashActivity.

Hope it helps!

Mikhail_Sam
  • 9,166
  • 11
  • 54
  • 87
phoenix
  • 496
  • 3
  • 11
1

you should disable Instant Run android studio Settings.

File>Settings>Build,Execution, Deployment>Instant Run unCheck all options shown there.

Note: White screen Issue due to Instant Run is only for Debug builds,this Issue will not appear on release builds.

Mina Farid
  • 3,355
  • 4
  • 30
  • 39
1

White background is caused because of the Android starts while the app loads on memory, and it can be avoided if you just add this 2 line of code under SplashTheme.

<item name="android:windowDisablePreview">true</item>
<item name="android:windowIsTranslucent">true</item>
Farid Haq
  • 2,960
  • 17
  • 14
1

This solved the problem :

Edit your styles.xml file :


Paste the code below :

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

</resources>

And don't forget to make the modifications in the AndroidManifest.xml file. (theme's name)

Be careful about the declaration order of the activities in this file.

Julien Jm
  • 1,650
  • 2
  • 17
  • 25
1
I encountered a similar problem and to overcome it, I implemented the code below in styles, i.e res->values->styles->resource tag 
<item name="android:windowDisablePreview">true</item>

Here is the whole code:

<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDisablePreview">true</item>
</style>
0

Try the following code:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <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>

This code works for me and will work on all Android devices.

Hitesh sapra
  • 254
  • 2
  • 12
  • Code-only answers are discouraged. Please click on [edit] and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. [From Review](https://stackoverflow.com/review/late-answers/22067254) – Nick Jan 30 '19 at 06:03
0

Its Solution is very Simple!

There are Three Basic Reasons for This problem

  1. You are doing Heavy / Long running / Complex task in onCreateVeiw Function.
  2. If your are using Thread. Then Thread Sleep time may be very large.
  3. If You are using any Third Party library. Which is initialize at app start up time it may leads this problem.

Solutions:

Solution 1:

  Remove the Heavy Task from onCreateView() function and place it some where appropriate place.

Solution 2:

  Reduce the Thread Sleep time.

Solution 3:

  Remove the Third party library at app initialize at implement them with some good strategy.

In my Case i am using Sugar ORM which leads this problem.

Share to improve.

Sheharyar Ejaz
  • 2,698
  • 1
  • 12
  • 14