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.
18 Answers
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----
}
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>
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">
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.
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.
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!
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>
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.
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!
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.
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>
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.
Its Solution is very Simple!
There are Three Basic Reasons for This problem
- You are doing Heavy / Long running / Complex task in onCreateVeiw Function.
- If your are using Thread. Then Thread Sleep time may be very large.
- 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.
onCreate
part. Try to just "setContentView" in that activity and check if this delay is gone. – Sherif elKhatib