Creating a Splash Screen in Android

In this tutorial you will learn how to make a simple splash screen. You will be using a CountDownTimer and will see how to add play audio using MediaPlayer.

Step 1: Create a launcher activity.

Create a launcher activity. Add your app’s logo ,tagline and anything else you would like to show on your splash screen in the xml layout file.

SplashScreen1

Step 2: Hiding the Action bar and status bar

Go to the Activity java file and use setFlags() method in the following manner:

        //This makes your activity fullscreen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

SplashScreen2

Go to Android Manifest file and add the following code to the activity tag to hide the action bar.

android:theme=“@style/Theme.AppCompat.DayNight.NoActionBar”

SplashScreen3

Step 3: Adding the CountDownTimer

Next go to the activity java file and create a new CountDownTimer instance and start the timer created.In the onFinish() method of the CountDownTimer you can start the next Activity that you want open.
The following sample code shows how the CountDownTimer works:


//Setting up the CountDownTimer for 2 sec with onTick method called every sec CountDownTimer ctd = new CountDownTimer(2000,1000) { @Override public void onTick(long millisUntilFinished) { //Every one second this method is called } @Override public void onFinish() { Intent nIntent = null; try { nIntent = new Intent(MainActivity.this,Class.forName("com.anuraag.myapplication.NewActivity")); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { if(nIntent!=null){ startActivity(nIntent); } } } }; //This will start the CountDownTimer at the end of which NewActivity is started. ctd.start();

SpalshScreen4

Step 4: Adding the Audio file to your application package

Put the audio file that you want to play in the raw folder which is inside the res folder.
If you don’t have the raw folder already there, you can create the raw folder by right clicking on the res folder and choosing a new android resource directory. Name it raw and select resource type also as raw. Then place the audio file in this directory.

SplashScreen5

Step 5: Create a MediaPlayer instance

Create a MediaPlayer instance and pass it the audio file that you added.
Then using that instance you can start the audio file.
The following code shows how to do this.


MediaPlayer mplayer = MediaPlayer.create(this,R.raw.startsound); //starts the audio file mplayer.start();

SplashScreen6

Step 6: Stop and release the audio file as you go to the next activity

You need to stop and release the audio before leaving this activity so as to make sure no errors creep in. To do this you could use the onFinish() method of the CountDownTimer.
The code would look like this after adding the stop() and release() methods to the onFinish() method of CountDownTimer:


//Setting up the CountDownTimer for 2 sec with onTick method called every sec CountDownTimer ctd = new CountDownTimer(2000,1000) { @Override public void onTick(long millisUntilFinished) { //Every one second this method is called } @Override public void onFinish() { Intent nIntent = null; try { nIntent = new Intent(MainActivity.this,Class.forName("com.anuraag.myapplication.NewActivity")); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { mplayer.stop(); mplayer.release(); if(nIntent!=null){ startActivity(nIntent); } } } }; ctd.start();

SplashScreen7

You can also add finish() method to the onFinish() method of the CountDownTimer to end the activity after new activity starts if you don’t want the user to come back to this activity when the back button is pressed.

SplashScreen8

After two seconds the next activity starts

SplashScreen9

This content has been helpful to you?

Thanks for contributing!

Yes No