Android Splash Screen Example

A splash screen is the first screen that appears when the user opens the application. There are different ways to create the android splash screen. We will create a sample android application to implement the splash screen in two different scenarios.

What will you learn?

  • How to create a simple Splash Screen in Android application
  • How to create a network operation based Splash Screen in Android application

Android Splash Screen Example App – Implementation

Step:1

Create a new project in Android Studio with default settings and named it whatever you like, in my case, it is SplashScreenTutrial.

Step:2

Open your Splash activity layout file and update it as per below code

We have a simple splash screen consisting of ProgressBar and TextView. It will show the loading spinner and a message “Loading…”. This layout will generate following output.

android splash screen example

Step:3

Now, We will add some code in MainActivity that will be holding Splash screen. Open your MainActivty.java file and add the below code

We are using Thread to wait for 4 seconds. There is a while loop that will keep running until flag value is equal to 4. Once it exits the while loop, finally block will close the MainActivity (splash screen) and open the ProfileActivity.

Step:4

Create a new activity and name it ProfileActivty. Add the below code in ProfileActivity.java.

Add the below code in the layout file of ProfileActivity. It is a very simple layout having a TextView.

Step:6

Finally, We have finished the first method. Run the application. MainActivity will remain active for 4 seconds and then after 4 seconds, ProfileActivty will appear.

android splash screen example android splash screen example

How to create a network operation based Splash Screen

We will make a network call to a server by using HttpURLConnection class in MainActivity. Splash screen will remain active until we receive the response from the server. We will update the TextView according to the response we get from the server.

We will update the MainActivity.java file and add some code for making network call by using HttpURLConnection and AsyncTask. We will update the TextView and launch the ProfileActivity once we get the response from server. Update the MainActivity.java according to below code.

Comment out the code we used in the first method.

Add the Internet permission in AndroidManifest.xml.

FetchDataFromServer() method calls the remote server by using httpURLConnection. It returns server response in the form of a string. If there is any error, it will return an empty string.

StreamToString() method converts the InputStream into String.

We have created an AsyncTask subclass (MakeNetworkCall) to make the network call in the background without blocking main thread or UI thread.

A complete tutorial on How to use HttpURLConnection to make network call

We start AsyncTask class by calling execute() method. This execute() method calls the doInBackground() and then onPostExecute() method. doInBackground() does all the processing in the background. Once doInBackground() is finshed, onPostExecute() will be called automatically.

If we get “OK” response from the server, we launch ProfileActivity. If we get empty string then we display the error message in TextView. We can update the UI in onPostExecute() method.

Download Source Code

Run the application and see the output. Have a look at below video. You can see the output of both above method and project structure.

I hope, you will like this tutorial. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …