Android Splash Screen Tutorial Using New Android Studio PDF


Android Splash Screen Example:

First we will create a new default project using these simple steps:

  1. Click on File > New Project.
  2. Next, define Application Name and Minimum SDK and hit Next
  3. Select Blank Activity and Hit Next.
  4. Hit  Finish.

    This creates a simple Hello world Project for which we will implement android Splash screen. We already discussed in detail about Creating New Android Project in our previous article.

    Creating Android Splash Screen:

    1. To display a splash screen, we need a layout first of all. So let’s create a Layout by Right Clicking on Layout (located in App > Res > Layout) and selecting New > Layout Resource File. Give it any name. I gave it splash.xml. Now select Text tab located at bottom as shown in image below.

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

      4. Splash screen is a separate activity which will be displayed first ahead of all other Activity. We create splash screen activity by creating a class. We create a new class by Selecting App > Java > Your Package Name and right Clicking on your package and selecting New > Java Class. Give it any name. Here, I gave it as SplashScreen. Now Type the Following Code in SplashScreen activity:


      import android.app.Activity;import android.content.Intent;public class SplashScreen extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub super.onCreate(savedInstanceState);        setContentView(R.layout.splash);        Thread timerThread = new Thread() {            public void run() {                try {                    sleep(3000);                } catch (InterruptedException e) {                    e.printStackTrace();                } finally {                    Intent intent = new Intent(SplashScreen.this, MainActivity.class);                    startActivity(intent);                }            }        };        timerThread.start();    }    @Override    protected void onPause() { // TODO Auto-generated method stub        super.onPause();        finish();    }}

      sleep takes the time in milliseconds as its parameter(3000 => 3 seconds). Here we used this delay time as the time to display the splash screen activity. After this delay time, MainActivity is started by the code written in finally{} block.

      The splash screen activity must not be shown when the user presses the back button. In order to do this, we should destroy the splash screen activity after it is shown for few seconds. This is done by the use of onPause() method. The onPause() method is a method of Activity class which comes into play when the user leaves the activity.

      Next, We need to define which activity to open after displaying Splash screen. This is done using the Intent(Context, Class) constructor of the Intent class.

      5. Now to make your splash screen to work, you need to refer the SplashScreen Activity in Android Manifest. So, open the Manifest file by clicking on App > Manifests > AndroidManifest.xml. Now we add reference to new Activity we just created and Change the Launcher Activity (Activity which Launches first) to splash.xml by changing the code shown below:

      android:name=".SplashScreen"android:name=".MainActivity"