How do I make a splash screen? [closed]
Asked Answered
W

31

619

I want my app to look more professional, so I decided to add a splash screen.

How should I go about the implementation?

Whilom answered 30/3, 2011 at 13:15 Comment(15)
Why does a application look more professional using a splash screen? I know no 'professional' android app which got one.Christianly
Agreed with @theomega. Splash screens are just plain annoying.Wriggly
You should only show a splash screen if you have background loading work to do. Otherwise, your app looks more "professional" when you give the user what they want from your app as quickly as possible. Users notice (and get annoyed by) delays in excess of 100ms, and you are an order of magnitude above this threshold by adding a splash screen.Cockahoop
kindle app, aldiko (reader), dolphin.. Umm the OS :) They all got a splash. Opera Mobile, Mantan Reader, Maps. I could go on. If it hides a load, then it at least let the user know your app has started. A delay of a few seconds is hidden much better when there's at least something of you, on the screen.Delisadelisle
I have given the answer at [@StackoverFlow][1] [1]: #19580776Inshrine
@theomega: Have you never played Angry Birds?Villon
IMO, splash screens are not useless when used to do some background loading or validation.Sometimes I have to wait for about 20 seconds for Candy Crush to load and I never thought about deleting it.Just don't abuse it.Pettifog
Splash screen gives you and excellent opportunity to advertise your game or app company name or logo. I like making the splash screen clickable so the user has the option of skipping ahead to the game. If the user always sees your company's logo for even half a second every time they open your app, they will be more likely to remember who the heck you are. Just make sure they are having a good experience with your app.Customary
see this github.com/meetmehdi/GoodSplashHomophonic
Google's official documentation is here: developer.android.com/topic/performance/vitals/…Irmgard
Here man android.jlelse.eu/…Cut
"I know no 'professional' android app which got one." I know tons of them: Whats App, Instagram, YouTube etc. They all have a splash screen.Gemagemara
@LukasNießen things were different 7 now 9 years ago.Emmerie
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Garment
Even after gaining more than 600 votes, this question has been closed...Savaii
V
547

Further reading:

Old answer:

HOW TO: Simple splash screen

This answers shows you how to display a splash screen for a fixed amount of time when your app starts for e.g. branding reasons. E.g. you might choose to show the splash screen for 3 seconds. However if you want to show the spash screen for a variable amount of time (e.g. app startup time) you should check out Abdullah's answer https://mcmap.net/q/64074/-how-do-i-make-a-splash-screen-closed. However be aware that app startup might be very fast on new devices so the user will just see a flash which is bad UX.

First you need to define the spash screen in your layout.xml file

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent">

          <ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:src="@drawable/splash"
                  android:layout_gravity="center"/>

          <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello World, splash"/>

  </LinearLayout>

And your activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class Splash extends Activity {

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splashscreen);

        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Splash.this,Menu.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

Thats all ;)

Virtu answered 30/3, 2011 at 13:28 Comment(20)
Sorry man... This works just on Android 4.0 and up. How do I make it working in Android 2.3.6? Thank you, I knwo this is old question but someone (like me) would need this. Thank youKutuzov
Sorry but it force closes.. here it is the error..(just a piece of it) FATAL EXCEPTION: main java.lang.NoClassDefFoundError: android.app.ActivityOptions at com.LCPInt.ittsvolta.List$1.<init>(List.java:31) at com.LCPInt.ittsvolta.List.onCreate(List.java:30)Kutuzov
@Kutuzov please create a question on SO for you problem an paste your whole error log.Virtu
Don't forget to add splash in ManifestInshrine
@Peter the question is not how to show a splash screen while loading data.Virtu
There is a big problem of this code, the splash screen also displayed each time the app resumed. how to make it only show up when startup? not resumed.Haemophilia
@Haemophilia on config change you save the state of your activity. So you can save the fact that you already displayed the splash screen.Virtu
@artworkadシ I can use a variable to record the splash screen already displayed, but I don't know how to disable it when resume.Haemophilia
@Haemophilia you have to disable it in onCreate. OnCreate is also called when the activity is recreated.Virtu
What is meaning of onCreate(Bundle icicle) ? what is icicle and can we change savedInstanceState to any thing?Cholinesterase
This is the most straightforward and simple splash screen tutorial I've found. Thanks for sharing!Gravamen
This is not a proper solution for a splash screen, it makes the user wait to show a splash screen, however splash screen's purpose is vice versa. It should show a splash screen while the user is waiting. Please see @Abdullah's solution.Georgiannageorgianne
@Georgiannageorgianne read the question. If you don't delay the splash screen how long would it display on a decent phone, a fraction of a second? It OP clearly stated that he wants to show the splash screen just to look professional and not because he is doing some e.g. REST API loading while app is starting.Virtu
many comments, yet nobody commented about the potential memory leak that lies in this solution. I highly recommend reading this blog post: codepond.org/2015/08/…Lagoon
Rather than stalling the app for SPLASH_DISPLAY_LENGTH time. You should do this instead: bignerdranch.com/blog/splash-screens-the-right-wayDillingham
Add android:noHistory="true" in AndroidManifest.xml to prevent the user from going back to the splash screen using the back button. from @RachelDolores
This works fine but we could see a blank screen flicker before this splash screen appears. Could you please tell me how to fix it. Thank youElaterin
@Elaterin this is not an issue with the splash screen, check this #20547203Virtu
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Garment
If your app is for Android Oreo or greater, note that there is a built-in and high-efficiency way to do that. See my another post for the explanation of it: https://mcmap.net/q/65379/-android-splashscreenGlossolalia
E
713

Note this solution will not let the user wait more: the delay of the splash screen depends on the start up time of the application.

When you open any android app you will get by default a some what black screen with the title and icon of the app on top, you can change that by using a style/theme.

First, create a style.xml in values folder and add a style to it.

<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

Instead of using @android:style/Theme.DeviceDefault.Light.NoActionBar you can use any other theme as a parent.

Second, in your app Manifest.xml add android:theme="@style/splashScreenTheme" to your main activity.

<activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:theme="@style/splashScreenTheme" >

Third, Update your theme in your onCreate() launch activity.

protected void onCreate(Bundle savedInstanceState) {
    // Make sure this is before calling super.onCreate
    setTheme(R.style.mainAppTheme);
    super.onCreate(savedInstanceState);
}

UPDATE Check out this post.

Thanks to @mat1h and @adelriosantiago

Extracellular answered 5/4, 2013 at 10:53 Comment(33)
is it correct to assume you need screen_shot.png in each of $APP/res/drawable-{l,m,h,xh}dpi/ ?Concision
thanks, sorry to bother you with that, I figured it out not long after I posted but forgot to delete.Concision
It seems the "parent" is only supported in API 14 and aboveGasconade
@Gasconade just change the parent theme to want you want and what is supportedExtracellular
This is the right way to do a splash screen. Thanks! The answer with more up votes with delays is just bad practice. Nothing should be delaying the user from seeing the first functional screen.Peekaboo
One problem I had with this is that <item name="android:background"> would override the windowBackground. And without android:background defined, my background in any fragments would be transparent revealing the activity behind the foreground content.Val
This should be the accepted answer. Whether someone will use the artificial delay like handlers or not, but it should be done with the theme introduced here splashScreenThemeAddams
@WilliamGrand you can use two themes one for the splash screen and them replace it with a normal one in your onCreateExtracellular
@Abdullah: I followed the way you said. It works fine, but the splash screen appears for few milliseconds on activity transitions.Myramyrah
Thanx, works great for me. Also you can use drawable-land and drawable-port folders to place your oriented splash screen, no need to add any code!Alfilaria
Is there a way to avoid the image changing its aspect ratio? My splash screen is a square, looks good on tablets but it looks really bad on phones.Aubreyaubrie
@Aubreyaubrie use different image sizes for different screensExtracellular
@Extracellular Thanks! That would be a possible solution, however I found this: plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd, to avoid creating different images for each screen size it is also possible to create a XML containing the image so that it will look fine on all screens.Aubreyaubrie
@Extracellular , thank you very much. Is it possbile to have animation in splash screen using your method ?Incapacity
@ArsenSench I never thought of this idea before. I'm not sure.Extracellular
only white background is shown in my case please help.Takao
How do we center content and what about different devices with content sizes etc?Elsewhere
Is it possible to add text views to this type of splash screens?Counterinsurgency
Any way to do scaling?Michale
@Myramyrah Yes, reasonable point. But you can use Thread.sleep(i);where i is e.g.1000 for a second delay.Right before super.onCreate.Dawndawna
This didn't work for me. The RecyclerView and drawer background now have the splash screen as the background.Biradial
@40-Love make sure you use check the third step.Extracellular
You are right. Thanks. I had set the theme as the splash screen theme, not as the app theme. It works now!Biradial
This is the right approach.Using a timer and other stupid ways are annoying this way saves everybody time and it's cool.Thank you for sharing.Meddle
But still your way is not complete I'll add my own answer probably check it out.Meddle
In your update link Google+ is shutdown, please share us the content of that broken linkEtoile
Don't forget android:opacity="opaque", so the full line in launch_background.xml looks like: <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">Mouthful
@WilliamGrand this will fix your problem of transparent background: android.jlelse.eu/…Shawndashawnee
NOTE: The background color of the splash screen won't change according to app's day/night theme, and always follows device's theme setting.Levileviable
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Garment
Attention not to mix up with android:background as this also affects the background of other views, not just to window background. In my case i had to display an AlertDialog when some dependencies were missing, and every component of the dialog had that backgroundTrollope
In newer Android API version style.xml is replaced with themes.xmlHairston
Can one add animated gif to windowBackground? or in related layer-list?Bureaucratize
V
547

Further reading:

Old answer:

HOW TO: Simple splash screen

This answers shows you how to display a splash screen for a fixed amount of time when your app starts for e.g. branding reasons. E.g. you might choose to show the splash screen for 3 seconds. However if you want to show the spash screen for a variable amount of time (e.g. app startup time) you should check out Abdullah's answer https://mcmap.net/q/64074/-how-do-i-make-a-splash-screen-closed. However be aware that app startup might be very fast on new devices so the user will just see a flash which is bad UX.

First you need to define the spash screen in your layout.xml file

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent">

          <ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:src="@drawable/splash"
                  android:layout_gravity="center"/>

          <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello World, splash"/>

  </LinearLayout>

And your activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class Splash extends Activity {

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splashscreen);

        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Splash.this,Menu.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

Thats all ;)

Virtu answered 30/3, 2011 at 13:28 Comment(20)
Sorry man... This works just on Android 4.0 and up. How do I make it working in Android 2.3.6? Thank you, I knwo this is old question but someone (like me) would need this. Thank youKutuzov
Sorry but it force closes.. here it is the error..(just a piece of it) FATAL EXCEPTION: main java.lang.NoClassDefFoundError: android.app.ActivityOptions at com.LCPInt.ittsvolta.List$1.<init>(List.java:31) at com.LCPInt.ittsvolta.List.onCreate(List.java:30)Kutuzov
@Kutuzov please create a question on SO for you problem an paste your whole error log.Virtu
Don't forget to add splash in ManifestInshrine
@Peter the question is not how to show a splash screen while loading data.Virtu
There is a big problem of this code, the splash screen also displayed each time the app resumed. how to make it only show up when startup? not resumed.Haemophilia
@Haemophilia on config change you save the state of your activity. So you can save the fact that you already displayed the splash screen.Virtu
@artworkadシ I can use a variable to record the splash screen already displayed, but I don't know how to disable it when resume.Haemophilia
@Haemophilia you have to disable it in onCreate. OnCreate is also called when the activity is recreated.Virtu
What is meaning of onCreate(Bundle icicle) ? what is icicle and can we change savedInstanceState to any thing?Cholinesterase
This is the most straightforward and simple splash screen tutorial I've found. Thanks for sharing!Gravamen
This is not a proper solution for a splash screen, it makes the user wait to show a splash screen, however splash screen's purpose is vice versa. It should show a splash screen while the user is waiting. Please see @Abdullah's solution.Georgiannageorgianne
@Georgiannageorgianne read the question. If you don't delay the splash screen how long would it display on a decent phone, a fraction of a second? It OP clearly stated that he wants to show the splash screen just to look professional and not because he is doing some e.g. REST API loading while app is starting.Virtu
many comments, yet nobody commented about the potential memory leak that lies in this solution. I highly recommend reading this blog post: codepond.org/2015/08/…Lagoon
Rather than stalling the app for SPLASH_DISPLAY_LENGTH time. You should do this instead: bignerdranch.com/blog/splash-screens-the-right-wayDillingham
Add android:noHistory="true" in AndroidManifest.xml to prevent the user from going back to the splash screen using the back button. from @RachelDolores
This works fine but we could see a blank screen flicker before this splash screen appears. Could you please tell me how to fix it. Thank youElaterin
@Elaterin this is not an issue with the splash screen, check this #20547203Virtu
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Garment
If your app is for Android Oreo or greater, note that there is a built-in and high-efficiency way to do that. See my another post for the explanation of it: https://mcmap.net/q/65379/-android-splashscreenGlossolalia
S
62
  • Create an activity: Splash
  • Create a layout XML file: splash.xml
  • Put UI components in the splash.xml layout so it looks how you want
  • your Splash.java may look like this:

    public class Splash extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            int secondsDelayed = 1;
            new Handler().postDelayed(new Runnable() {
                    public void run() {
                            startActivity(new Intent(Splash.this, ActivityB.class));
                            finish();
                    }
            }, secondsDelayed * 1000);
        }
    }
    
  • change ActivityB.class to whichever activity you want to start after the splash screen

  • check your manifest file and it should look like

        <activity
            android:name=".HomeScreen"
            android:label="@string/app_name">     
        </activity>

        <activity
            android:name=".Splash"
            android:label="@string/title_activity_splash_screen">     
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Sorrells answered 30/3, 2011 at 13:21 Comment(6)
This is not the purpose of splash screen. This makes extra 1 second delay. Splash screen should be an image while loading first screen of the application. This link may help. https://mcmap.net/q/65379/-android-splashscreenGeorgiannageorgianne
@efeyc: you are 100% right .. yet, it looks pretty nice when app starts .. don't you think ?Bludgeon
@Suda.nese definitely not. Users don't want to look at a picture, users want to use the app and not have an unecessary delaySyriac
@TimCastelijns It depends on the app under development and what is the splash screen looks like .. of course it is meant to be practical, but who says it cannot be used otherwise !!Bludgeon
Agreed @Suda.nese If the app requirements include a splash screen, than a splash screen it is! Sure, it may not be desirable for users, but if a client wants a splash screen, then by gosh give it to themSorrells
Check my optimal and easy solution: medium.com/@vatani.ahmad/…Garment
H
30

The answers above is very good, but I would like to add something else. I am new to Android, I met these problem during my development. hope this can help someone like me.

  1. The Splash screen is the entry point of my app, so add the following lines in AndroidManifest.xml.

        <activity
            android:name=".SplashActivity"
            android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
  2. The splash screen should only show once in the app life cycle, I use a boolean variable to record the state of the splash screen, and only show it on the first time.

    public class SplashActivity extends Activity {
        private static boolean splashLoaded = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if (!splashLoaded) {
                setContentView(R.layout.activity_splash);
                int secondsDelayed = 1;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    }
                }, secondsDelayed * 500);
    
                splashLoaded = true;
            }
            else {
                Intent goToMainActivity = new Intent(SplashActivity.this, MainActivity.class);
                goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(goToMainActivity);
                finish();
            }
        }
    }
    

happy coding!

Haemophilia answered 22/12, 2014 at 8:30 Comment(1)
You can add android:noHistory="true" in AndroidManifest.xml to prevent the user from going back to the splash screen using the back button.Nahamas
U
23

Abdullah's answer is great. But i want to add some more details to it with my answer.

Implementing a Splash Screen

Implementing a splash screen the right way is a little different than you might imagine. The splash view that you see has to be ready immediately, even before you can inflate a layout file in your splash activity.

So you will not use a layout file. Instead, specify your splash screen’s background as the activity’s theme background. To do this, first create an XML drawable in res/drawable.

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Background Color -->
    <item
        android:drawable="@color/gray"/>

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

</layer-list>

It just a layerlist with logo in center background color with it.

Now open styles.xml and add this style

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

This theme will have no actionbar and with background that we just created above.

And in manifest you need to set SplashTheme to activity that you want to use as splash.

<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>

Then inside your activity code navigate user to the specific screen after splash using intent.

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

That's the right way to do. I used these references for answer.

  1. https://material.google.com/patterns/launch-screens.html
  2. https://www.bignerdranch.com/blog/splash-screens-the-right-way/ Thanks to these guys for pushing me into right direction. I want to help others because accepted answer isn't a recommended to do splash screen.
Ungainly answered 6/8, 2016 at 3:55 Comment(6)
I saw a tutorial on YouTube regarding about this. But I think the bitmap size will be the problem since you cannot resize it using layer-list .Carabin
you can resize the item using width and height property for 23+ API but ideally you you should upload different size images for each screen resolution (e.g. hdpi, xxhdpi)Ungainly
The Implementing a Splash Screen section is very useful, thanks!Groark
Never use <bitmap> tag anymore this can raise ResourceNotFound exception; instead use android:drawable instead of android:src in the <itme> directlyPresley
Can one add animated gif to windowBackground? or in related layer-list?Bureaucratize
For those interested: According to this post, it is not possible to use a full screen image anymore for Android version >12: https://mcmap.net/q/65380/-ionic-capacitor-splash-screen-shows-wrong-imageTurnout
G
13
  1. Create an Activity SplashScreen.java

    public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 3000; // time to display the splash screen in ms
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
    
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (_active && (waited < _splashTime)) {
                        sleep(100);
                        if (_active) {
                            waited += 100;
                        }
                    }
                } catch (Exception e) {
    
                } finally {
    
                    startActivity(new Intent(SplashScreen.this,
                            MainActivity.class));
                    finish();
                }
            };
                 };
        splashTread.start();
    }
     }
    
  2. splashscreen.xml will be like this

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="600px" android:layout_height="1024px"
      android:background="#FF0000">
    </RelativeLayout> 
    
Grith answered 28/3, 2012 at 6:16 Comment(0)
J
13

A Splash Screnn, by default, does not automatically make your Application look more professional. A professionally designed Splash Screen has a possibility of making your Application look more professional, but if you do not know how to write one then how professional will the rest of your Application actually be.

About the only reason (excuse) to have a Splash Screen is because you are doing a massive amount of Calculations or are waiting for GPS/WiFi to startup because your Application relies on that prior to it starting. Without the result of those Calculations or access to GPS/WiFi (etc.) your Application is dead in the water, thus you feel you need a Splash Screen, and MUST block the view of the Screen for any other running Programs (including the Background).

Such a Splash Screen ought to look like your Full Screen Application to give the impression that it has already initialized, then after the lengthy calculations are completed the final details could be filled in (the Image tweaked). The chance of that being the case or that it is the only way the Program could be designed is mighty small.

It would be better to allow the User (and the rest of the OS) to do something else while they wait rather than design your Program to be dependant on something that will take a while (when the duration of the wait is uncertain).

There are Icons on your Phone already that say that GPS/WiFi is starting. The time or space taken up by the Splash Screen could be spent loading pre-calculations or actually doing the Calculations. See the first Link below for the problems you create and what must be considered.

If you absolutely must wait for these Calculations or GPS/WiFi it would be best to simply let the Application start and have a pop-up that says that it is necessary to wait for the Calculations (a TEXTUAL "Initializing" Message is fine). The wait for GPS/WiFi is expected (if they were not enabled in another Program already) so announcing their wait times are unnecessary.

Remember that when the Splash Screen starts your Program IS actually running already, all you are doing is delaying the use of your Program and hogging the CPU/GPU to do something that most do not feel is necessary.

We had better really want to wait and see your Splash Screen every time we start your Program or WE will not feel it is very professionally written. Making the Splash Screen FULL Screen and a duplicate of the actual Program's Screen (so we think it is initialized when in fact it has not) MIGHT accomplish your goal (of making your Program look more professional) but I would not bet much on that.

Why not to do it: http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/

How to do it: https://encrypted.google.com/search?q=Android+splash+screen+source

So there is a good reason not to do it but IF you are certain that somehow your situation falls outside those examples then the means to do it is given above. Be certain that it really does make your Application look more professional or you have defeated the only reason you gave for doing this.

It is like a YouTube Channel that starts every Video with a lengthy Graphic Intro (and Outro) or feels the need to tell a Joke or explain what happened during the past week (when it is not a Comedy or LifeStyles Channel). Just show the show ! (Just run the Program).

Joettejoey answered 21/5, 2014 at 1:3 Comment(0)
A
12

Above all answers are really very good. But there are encounter problem of memory leakage. This issue is often known in the Android community as "Leaking an Activity". Now what exactly does that mean?

When configuration change occurs, such as orientation change, Android destroys the Activity and recreates it. Normally, the Garbage Collector will just clear the allocated memory of the old Activity instance and we're all good.

"Leaking an Activity" refers to the situation where the Garbage Collector cannot clear the allocated memory of the old Activity instance since it's being (strong) referenced from an object that out lived the Activity instance. Every Android app has a specific amount of memory allocated for it. When Garbage Collector cannot free up unused memory, the app's performance will decrease gradually and eventually crash with OutOfMemory error.

How to determine whether the app leaks memory or not? The fastest way is to open the Memory tab in Android Studio and pay attention to allocated memory as you change the orientation. If the allocated memory keeps on increasing and never decreases then you have a memory leak.

1.Memory leak when user change the orientation. enter image description here

First you need to define the splash screen in your layout resource splashscreen.xml file

Sample Code for splash screen activity.

public class Splash extends Activity {
 // 1. Create a static nested class that extends Runnable to start the main Activity
    private static class StartMainActivityRunnable implements Runnable {
        // 2. Make sure we keep the source Activity as a WeakReference (more on that later)
        private WeakReference mActivity;

        private StartMainActivityRunnable(Activity activity) {
         mActivity = new WeakReference(activity);
        }

        @Override
        public void run() {
         // 3. Check that the reference is valid and execute the code
            if (mActivity.get() != null) {
             Activity activity = mActivity.get();
             Intent mainIntent = new Intent(activity, MainActivity.class);
             activity.startActivity(mainIntent);
             activity.finish();
            }
        }
    }

    /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 1000;

    // 4. Declare the Handler as a member variable
    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(icicle);
        setContentView(R.layout.splashscreen);

        // 5. Pass a new instance of StartMainActivityRunnable with reference to 'this'.
        mHandler.postDelayed(new StartMainActivityRunnable(this), SPLASH_DISPLAY_LENGTH);
    }

    // 6. Override onDestroy()
    @Override
    public void onDestroy() {
     // 7. Remove any delayed Runnable(s) and prevent them from executing.
     mHandler.removeCallbacksAndMessages(null);

     // 8. Eagerly clear mHandler allocated memory
     mHandler = null;
    }
}

For more information please go through this link

Astto answered 18/2, 2016 at 1:55 Comment(0)
W
4

This is the full code here

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

private final int SPLASH_DISPLAY_DURATION = 1000;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);


    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {

            Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
            SplashActivity.this.startActivity(mainIntent);
            SplashActivity.this.finish();
        }
    }, SPLASH_DISPLAY_DURATION);
}}

In drawables create this bg_splash.xml

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

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

<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/ic_in_app_logo_big"/>
</item></layer-list>

In styles.xml create a custom theme

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

and finally in AndroidManifest.xml specify the theme to your activity

<activity
        android:name=".activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Cheers.

Waters answered 12/2, 2016 at 21:35 Comment(2)
How would I add an xml file instead of drawableElaterin
I mean you'll have to create bg_splash.xml in the drawable directory as described above.Waters
M
4

Splash screens should not be loaded from a layout file, there might still be some lag when loading it.

The best way is to create a Theme just for your SplashScreenActivity and set the android:windowBackground to a drawable ressource.

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

In a nutshell:

Declare your SplashScreenActivity in the manifest:

<activity
        android:name=".activities.SplashScreenActivity"
        android:theme="@style/SplashTheme"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In your SplashScreenActivity.java:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity_.class);
    startActivity(intent);
    finish();

}

Next create the ressource for the background window of your theme:

<style name="SplashTheme" parent="Theme.Bumpfie.Base">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

Drawable file splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/app_logo"/>
    </item>
</layer-list>
Menard answered 11/8, 2016 at 19:2 Comment(0)
A
4

After Android Marshmallow, other Productive use of Splash screen I come to think of is requesting necessary Android Permissions in your app's splash screen.

it seems like most apps handle permission request this way.

  • Dialogs make bad UIX and they break the main flow and make you decide on runtime and truth is most users might not even care if your app want to write something on SD card. Some of them might not even understand what we are trying to convey until we translate it in plain english.

  • Requesting permissions at one time make less number of "if else" before every operation and make your code looks clutter free.

This is a example of how you can ask for permissions in your splash activity for device running Android OS 23+ .

If all permissions are granted OR already granted OR app is running on Pre Marshmallow THEN just go and display main contents with little delay of half second so that user can appreciate effort we had put in reading this question and trying to give our best.

import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.c2h5oh.beer.R;
import com.c2h5oh.beer.utils.Animatrix;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SplashActivity extends AppCompatActivity {

    final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        //show animations 
        Animatrix.scale(findViewById(R.id.title_play), 100);
        Animatrix.scale(findViewById(R.id.title_edit), 100);
        Animatrix.scale(findViewById(R.id.title_record), 100);
        Animatrix.scale(findViewById(R.id.title_share), 100);

        if (Build.VERSION.SDK_INT >= 23) {

            // Marshmallow+ Permission APIs
            fuckMarshMallow();

        } else {

            // Pre-Marshmallow
            ///Display main contents
            displaySplashScreen();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
                Map<String, Integer> perms = new HashMap<String, Integer>();
                // Initial
                perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.MODIFY_AUDIO_SETTINGS, PackageManager.PERMISSION_GRANTED);
                perms.put(Manifest.permission.VIBRATE, PackageManager.PERMISSION_GRANTED);
                // Fill with results
                for (int i = 0; i < permissions.length; i++)
                    perms.put(permissions[i], grantResults[i]);

                // Check for ACCESS_FINE_LOCATION
                if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_GRANTED
                        && perms.get(Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED) {
                    // All Permissions Granted

                    // Permission Denied
                    Toast.makeText(SplashActivity.this, "All Permission GRANTED !! Thank You :)", Toast.LENGTH_SHORT)
                            .show();

                    displaySplashScreen();

                } else {
                    // Permission Denied
                    Toast.makeText(SplashActivity.this, "One or More Permissions are DENIED Exiting App :(", Toast.LENGTH_SHORT)
                            .show();

                    finish();
                }
            }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    @TargetApi(Build.VERSION_CODES.M)
    private void fuckMarshMallow() {
        List<String> permissionsNeeded = new ArrayList<String>();

        final List<String> permissionsList = new ArrayList<String>();
        if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
            permissionsNeeded.add("Read SD Card");
        if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
            permissionsNeeded.add("Record Audio");
        if (!addPermission(permissionsList, Manifest.permission.MODIFY_AUDIO_SETTINGS))
            permissionsNeeded.add("Equilizer");
        if (!addPermission(permissionsList, Manifest.permission.VIBRATE))
            permissionsNeeded.add("Vibrate");

        if (permissionsList.size() > 0) {
            if (permissionsNeeded.size() > 0) {

                // Need Rationale
                String message = "App need access to " + permissionsNeeded.get(0);

                for (int i = 1; i < permissionsNeeded.size(); i++)
                    message = message + ", " + permissionsNeeded.get(i);

                showMessageOKCancel(message,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                            }
                        });
                return;
            }
            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            return;
        }

        Toast.makeText(SplashActivity.this, "No new Permission Required- Launching App .You are Awesome!!", Toast.LENGTH_SHORT)
                .show();

        displaySplashScreen();
    }


    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(SplashActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

    @TargetApi(Build.VERSION_CODES.M)
    private boolean addPermission(List<String> permissionsList, String permission) {

        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
        }
        return true;
    }

    /**
     * Display main content with little delay just so that user can see
     * efforts I put to make this page
     */
    private void displaySplashScreen() {
        new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, AudioPlayerActivity.class));
                finish();
            }
        }, 500);
    }


}
Amerind answered 17/10, 2016 at 3:52 Comment(0)
M
3
public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread t=new Thread()
    {

        public void run()
        {   

            try {

                sleep(2000);
                finish();
                Intent cv=new Intent(MainActivity.this,HomeScreen.class/*otherclass*/);
                startActivity(cv);
            } 

            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    t.start();
}
Monjan answered 17/2, 2013 at 19:52 Comment(0)
C
2

Create a Activity, let us Activity named 'A', then create a xml file called myscreen.xml, in that set a the splash screen image as background, and then use count down timer to navigate from one Activtity to another. To know how to use Count Down timer see my answer in this question TimerTask in Android?

Canvass answered 30/3, 2011 at 13:24 Comment(0)
B
2

Splash screen example :

public class MainActivity extends Activity {
    private ImageView splashImageView;
    boolean splashloading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splashImageView = new ImageView(this);
        splashImageView.setScaleType(ScaleType.FIT_XY);
        splashImageView.setImageResource(R.drawable.ic_launcher);
        setContentView(splashImageView);
        splashloading = true;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                setContentView(R.layout.activity_main);
            }

        }, 3000);

    }

}
Busoni answered 9/6, 2014 at 7:7 Comment(0)
S
2

Splash screen is a little unusable object in Android: it can not be loaded as soon as possible for hiding the delay of main activity starting. There are two reasons to use it: advertising and network operations.

Implementation as dialog makes jump without delay from splash screen to main UI of activity.

public class SplashDialog extends Dialog {
    ImageView splashscreen;
    SplashLoader loader;
    int splashTime = 4000;

    public SplashDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setCancelable(false);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                cancel();
            }
        }, splashTime);

    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

    <ImageView
        android:id="@+id/splashscreen"
        android:layout_width="190dp"
        android:layout_height="190dp"
        android:background="@drawable/whistle"
        android:layout_centerInParent="true" />

</RelativeLayout>

And start:

public class MyActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getCategories() != null &&  getIntent().getCategories().contains("android.intent.category.LAUNCHER")) {
            showSplashScreen();
        }
    }

    protected Dialog splashDialog;
    protected void showSplashScreen() {
        splashDialog = new SplashDialog(this, R.style.SplashScreen);
        splashDialog.show();
    }

    ...
}
Shute answered 9/4, 2015 at 18:19 Comment(1)
There is a beneficiary for this approach and that is you can launch the MainActivity directly. For example when you use push notification in your app, when you are launching to your app by clicking on the notification, in this manner you can manage better the notification purposes.Ebba
S
2

Another approach is achieved by using CountDownTimer

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.splashscreen);

 new CountDownTimer(5000, 1000) { //5 seconds
      public void onTick(long millisUntilFinished) {
          mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
      }

     public void onFinish() {
          startActivity(new Intent(SplashActivity.this, MainActivity.class));
          finish();
     }

  }.start();
}
Saponify answered 10/4, 2016 at 19:5 Comment(0)
E
2

Sometime user open the SplashActivity and quit immediately but the app still go to MainActivity after SPLASH_SCREEN_DISPLAY_LENGTH.

For prevent it: In SplashActivity you should check the SplashActivity is finishing or not before move to MainActivity

public class SplashActivity extends Activity {

    private final int SPLASH_SCREEN_DISPLAY_LENGTH = 2000;

    @Override
    public void onCreate(Bundle icicle) {
        ...
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if (!isFinishing()) {//isFinishing(): If the activity is finishing, returns true; else returns false.
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }

            }, SPLASH_SCREEN_DISPLAY_LENGTH);
        }                             
   }                                
}

Hope this help

Erudition answered 24/5, 2016 at 3:37 Comment(0)
P
2
     - Add in SplashActivity 

   public class SplashActivity extends Activity {

       private ProgressBar progressBar;
       int i=0;
       Context context;
       private GoogleApiClient googleApiClient;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_splash);
           context = this;

           new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                   startActivity(new Intent(Splash.this, LoginActivity.class));
                   finish();
               }
           }, 2000);

       }

   }

  - Add in activity_splash.xml

   <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       xmlns:custom="http://schemas.android.com/apk/res-auto"
       android:background="@color/colorAccent"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".Splash">

       <ImageView
           android:id="@+id/ivLogo"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:src="@mipmap/icon_splash"
           android:layout_centerHorizontal="true"
           android:layout_centerVertical="true"/>


       <ProgressBar
           android:id="@+id/circle_progress"
           style="?android:attr/progressBarStyleHorizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_marginBottom="5dp"
           android:max="100"
           android:progressTint="@color/green"
           android:visibility="visible" />

   </RelativeLayout>

  - Add in AndroidManifest.xml

    <activity android:name="ex.com.SplashActivity">
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />

                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
Peppers answered 23/2, 2018 at 12:15 Comment(2)
Could you also add some explanation please?Egidio
Yeah sure... what explanation you want let me know please.Peppers
T
2

Really easy & gr8 approach :

Enjoy!

There are enough answers here that will help with the implementation. this post was meant to help others with the first step of creating the splash screen!

Tanana answered 25/6, 2018 at 7:19 Comment(0)
P
2

In Kotlin write this code:-

 Handler().postDelayed({
            val mainIntent = Intent(this@SplashActivity, LoginActivity::class.java)
            startActivity(mainIntent)
            finish()
        }, 500)

Hope this will help you.Thanks........

Priedieu answered 3/7, 2019 at 6:4 Comment(0)
B
1

How about a super-flexible launch screen that can use the same code and is defined in the AndroidManifest.xml, so the code will never need to change. I generally develop libraries of code and do not like customizing code because it is sloppy.

<activity
        android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data android:name="launch_class" android:value="com.mypackage.MyFirstActivity" />
        <meta-data android:name="duration" android:value="5000" />
</activity>

Then the SpashActivity itself looks up the meta-data for "launch_class" to then make the Intent itself. The meta data "duration" defines how long the splash screen stays up.

public class SplashActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    ComponentName componentName = new ComponentName(this, this.getClass());

    try {
        Bundle bundle = null;
        bundle = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA).metaData;
        String launch_class = bundle.getString("launch_class");
        //default of 2 seconds, otherwise defined in manifest
        int duration = bundle.getInt("duration", 2000);

        if(launch_class != null) {
            try {
                final Class<?> c = Class.forName(launch_class);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(SplashActivity.this, c);
                        startActivity(intent);
                        finish();
                    }
                }, duration);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
  }
}
Bahr answered 25/6, 2015 at 21:35 Comment(0)
D
1

Though there are good answers, I will show the google recommended way:

1)First create a Theme for splash screen: you have a theme called splashscreenTheme, your launcher theme would be:

<style name="splashscreenTheme">
  <item name="android:windowBackground">@drawable/launch_screen</item>
</style>

Note:

android:windowBackground already sets your splashscreen image no
need to do this in UI again.

you can also use color here instead of a drawable.

2)Set the theme to manifest of splashscreenActivity

   <activity
            android:name=".activity.splashscreenActivity"
            android:screenOrientation="portrait"
            android:theme="@style/splashscreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

3)make sure you launch_screen drawable is not in drawable folder if your image is not small.

It will result in faster launch screen start and save you from the black screen

It also avoids extra overdraw

Dorelle answered 13/6, 2016 at 11:10 Comment(0)
M
1

This is the best post I've seen on splash screens: http://saulmm.github.io/avoding-android-cold-starts

Saúl Molinero goes into two different options for splash screens: Taking advantage of the window background to animate into your initial screen and displaying placeholder UI (which is a popular choice that Google uses for most of their apps these days).

I refer to this post every time I need to consider cold start time and avoiding user dropoff due to long startup times.

Hope this helps!

Mooneye answered 16/7, 2016 at 20:52 Comment(0)
D
1

In my case I didn't want to create a new Activity only to show a image for 2 seconds. When starting my MainAvtivity, images gets loaded into holders using picasso, I know that this takes about 1 second to load so I decided to do the following inside my MainActivity OnCreate:

splashImage = (ImageView) findViewById(R.id.spllll);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            splashImage.setVisibility(View.GONE);

        }
    }, secondsDelayed * 2000);

When starting the application the first thing that happens is the ImageView gets displayed and the statusBar is removed by setting the window flags to full screen. Then I used a Handler to run for 2 seconds, after the 2 seconds I clear the full screen flags and set the visibility of the ImageView to GONE. Easy, simple, effective.

Dredge answered 16/11, 2017 at 5:20 Comment(0)
N
1

Its really simple in android we just use handler concept to implement the splash screen

In your SplashScreenActivity java file paste this code.

In your SplashScreenActivity xml file put any picture using imageview.

public void LoadScreen() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {                 
                Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
                startActivity(i);
            }
        }, 2000);
    }
Nance answered 29/3, 2018 at 7:19 Comment(0)
S
1

You can add this in your onCreate Method

new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // going to next activity
                    Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
                    startActivity(i);
                    finish();
                }
            },time);

And initialize your time value in milliseconds as yo want...

private  static int time=5000;

for more detail download full code from this link...

https://github.com/Mr-Perfectt/Splash-Screen

Stradivarius answered 2/4, 2019 at 19:23 Comment(0)
A
1

Here is a simple one!

~Lunox

MainActivity.java

package com.example.splashscreen;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

splashscreen.java

package com.example.splashscreen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class splashscreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        //Splash Screen duration
        int secondsDelayed = 1;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(splashscreen.this, MainActivity.class));
                finish();
            }
        }, secondsDelayed * 3000);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashlogo"

    />

splashlogo.png

splashlogo.png

GitHub

SplashScreen

Afc answered 16/11, 2019 at 8:59 Comment(0)
D
1

One way is by creating a FullScreenActivity/EmptyActivity (say SplashScreenActivity) and set it as the first Activity that shows up when you open your app. Add the following to your Activity in the AndroidManifest.xml

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

You can then set a handler to dismiss this activity after a few seconds.

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i= new Intent(SplashScreenActivity.this,MainActivity.class);
                startActivity(i); //start new activity 
                finish();
            }
        }, 3000); //time in milliseconds

Secondly, if you don't wish to create a separate activity, you can inflate a layout over your MainActivity and set the layout visibility to GONE or inflate the main layout over the existing splash screen layout after a few milliseconds.

Dieldrin answered 18/9, 2020 at 3:0 Comment(0)
N
0

Simple Code, it works:) Simple splash

int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(LoginSuccessFull.this, LoginActivity.class));
            finish();
        }
    }, secondsDelayed * 1500);
Nance answered 12/12, 2016 at 12:9 Comment(0)
U
0
public class SplashActivity extends Activity {

  Context ctx;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ctx = this;
      setContentView(R.layout.activity_splash);

      Thread thread = new Thread(){
          public void run(){
              try {
                  sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }

              Intent in = new Intent(ctx,MainActivity.class);
              startActivity(in);
              finish();
          }
      };
      thread.start();
  }
}
Unfaithful answered 31/3, 2017 at 11:11 Comment(1)
This is a bad example - what happens if you exit the SplashActivity by pressing back? The thread will still execute.Erection
B
0

I used threads to make the Flash Screen in android.

    import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class HomeScreen extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_home);

        Thread thread = new Thread(){
            public void run(){
                try {
                    Thread.sleep(3 * 1000);
                    Intent i = new Intent(HomeScreen.this, MainActivity.class);
                    startActivity(i);
                } catch (InterruptedException e) {
                }
            }
        };
        thread.start();
    }
}
Backbone answered 6/1, 2018 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.