Android | Creating a Splash Screen

A splash screen is mostly the first screen of the app when it is opened. It is a constant screen which appears for a specific amount of time, generally shows for the first time when the app is launched. The Splash screen is used to display some basic introductory information such as the company logo, content, etc just before the app loads completely.

Creating Splash screen using handler in Android

Here we created two activities MainActivity showing the Splash Screen and SecondActivity in order to switch from MainActivity to SecondActivity. The main program is written in MainActivity, you can change activities as per your need.



  • To remove the ActionBar, you need to make following changes in your styles.xml file.
    style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
    ...
    
  • Use colors which is suitable for your application.
  • No need to make any changes in your manifest file.

  • Using the ‘postDelayed()’ function:

    public final boolean postDelayed(Runnable Object token, long delayMillisec)

    This function delays the process for a specified time. This is used with a handler which allows you to send and process Message and Runnable objects associated with a Thread’s MessageQueue. Each handler instance is a single thread.

    Below is the code for creating the splash screen:

    MainActivity.java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.hp.splashscreen;
      
    import android.content.Intent;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.WindowManager;
      
    public class MainActivity extends AppCompatActivity {
      
        private static int SPLASH_SCREEN_TIME_OUT=2000;
        #After completion of 2000 ms, the next activity will get started.
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
      
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
            //This method is used so that your splash activity
            //can cover the entire screen.
              
            setContentView(R.layout.activity_main);
            //this will bind your MainActivity.class file with activity_main.
              
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i=new Intent(MainActivity.this,
                                             SecondActivity.class);
                    //Intent is used to switch from one activity to another.
                      
                    startActivity(i);
                    //invoke the SecondActivity.
                      
                    finish();
                    //the current activity will get finished.
                }
            }, SPLASH_SCREEN_TIME_OUT);
        }
    }

    chevron_right

    
    

    activity_main.xml: You can use any image for the splash screen and first paste it into the drawable folder. XML file is easy to generate by drag and drop approach, just use imageview and select the appropriate image.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.hp.splashscreen.MainActivity">
      
      
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="250dp"
            android:layout_height="200dp"
            app:srcCompat="@drawable/geeks"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintVertical_bias="0.447" />
    </android.support.constraint.ConstraintLayout>

    chevron_right

    
    

    Output:




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :
    Practice Tags :


    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.