Monday, March 4, 2019

Create Splash Activity in Android Studio


What is Splash activity

Splash Activity is nothing but an activity which pops up whenever an android application starts running i.e. whenever you open the application. For example, apps like Facebook, Twitter, etc are having this kind of activities. I am hereby, attaching screenshots of splash activities of both Facebook and twitter so you can get the exact idea.


facebook splash activity
Facebook Splash Activity

Twitter Splash Activity
Twitter Splash Activity (Night Mode)

Well you might be wondering that apart from these two apps, many applications are not having such kind of activity. To be clear, an activity is something like whatever user sees in application i.e. User Interface. It includes background process too, which are obviously invisible to user. So talking about Splash activity, it is not mandatory that every android application should have activity like this. It depends on developer and applications's requirements.

How we can create Splash activity

Let's assume that you are working on a project and now you want to add splash activity in your project. So you have to replace the very first activity of your app with your current MainActivity. You can do the same by editing AndroidManifest.xml file, which we will do later onward. Now, create layout resource file using following steps: Go to layoutNew Layout resource file.

Now, create the design for splash activity whatever you want, just like any normal layout xml file. Now create a java file which will set the content of your newly created xml file for splash activity and after sometime it will launch the main activity of your application.

splash_activity.xml:


MainActivity.java:



You can go through the java code which contains comments regarding functionalities of respective code snippets. Here, you might be wondering that why I have used thread in the code. As you know that, threads are used so that application keeps running multiple threads (threads - individual execution of a program) concurrently. So here, we want Splash Activity to run for some particular amount of time or duration. Here it is provided by a static variable, SPLASH_ACTIVITY_TIMER. Which is 9000 i.e. 9000 milliseconds. i.e Splash Activity will be visible for 9 seconds and after that Main Activity will be launched.

Another thing, if you have noticed that most of the applications' splash activity doesn't contain title bar or action bar. So, to do that we are using requestWindowFeature() method and parameters of WindowManager class. 

One thing to keep in mind that don't forget to finish the thread which runs the Splash Activity, after launching the MainActivity.

Most crucial part of this (AndroidManifest.xml), make changes as per shown below:


These changes are required because before this your application would show the MainActivity directly as initially MainActivity was your main activity. But now it's replaced with your Splash Activity. So the final output will look like this,

Splash Activity 

Hope you get this. If you have any doubts or queries then let me know in the comment section. :D  


No comments:

Post a Comment