What are splash screens and where can they be used?
6 minute(s) read | Published on: May 04, 2021 Updated on: Dec 14, 2021 |
It refers to the application's main page, to these logos and pages that reject the beginning of the path. Application applications are displayed called startup screens. Startup screens are a page that is displayed to users for a certain period at the beginning of launching applications, which includes the logo for the application, the manufacturer, or ... This page is an activity that includes a special logo or emblem that is displayed for advertising and introduction to users at the beginning of launching the application and is also determined for this activity to be displayed for a certain period and then disappear and go to the main page Transfer the application.
What are startup screens?
Splash screens are pages or activities that appear after launching an application and can even include an audio file. It is better to choose the audio and video files to be placed on the splash screen to be relevant to your wishes. You can do ads and introductions on the splash screen and put them in the view of users so that users can see them.
How to create startup screens
1- Create a new project with API 10 and choose the name SplashScreen as desired.
2- The activity used in this training is Empty Activity.
3- After creating the project, our project has MainActivity. We need to create another activity and assign one to the splash screen.
4- We create a new activity and assign it to the startup screen; we also assign the MainActivity to the application's main page.
5- We create a new activity of Empty Activity type called SplashActivity, which is the desired name.
6- Java class and active XML file are added to the project.
7- To create a startup screen to be displayed to users on the first page for a while when launching the application, we must use the Handler class.
8- We update the Handler class.
9- The class must be imported.
10- To determine the stop time, we use the postDelayed method.
11- The first parameter of this method is Runnable, and the second parameter is the desired time, each of which must be entered in milliseconds.
12- New Runnable and select the first option. By doing this, Android Studio will automatically create the run method.
13- The code that should be in the SplashScreen.java class is as follows:
14- After performing these steps, you may encounter an error due to not specifying the time.
15- After Runnable, we put a comma and enter the desired time. In this tutorial, we have entered the number 3000, that is, for three seconds.
16- You may encounter another error again due to forgetting Cologne.
17- It should be noted that after the splash screen display time, we must use intent to determine which page the user will be transferred to.
18- In other words, our goal is to move from SplashActivity to MainActivity after a specified period.
19- Complete the run method as follows:
20- Move the intent-filter related to MainActivity to SplashActivity:
21- We will implement the project.
22- After running the project, the result will be that first, the Splash Screen will be displayed for three seconds, and then it will be transferred to the MainActivity.
23- The result is successful. But after running the project, you will notice that by pressing the Back button, it will be transferred to the splash screen again instead of leaving the application completely.
24- To be able to apply the settings so that by leaving the Back button, it will exit the application, we must use the finish commands. Finish commands mean that the splash screen closes completely and does not appear after elapsed specified display time.
25- We will solve this problem as follows:
26- By adding the finish command, we cause the activity to be closed after a certain period, and it is impossible to return to it again.
27- After we were able to run the splash screen successfully, we want to change its appearance as we wish.
28- As we have said before, in Android Studio, to use images, we have to put the images inside the drawable. The images that need to be added to Android studio projects are placed inside the drawable folder.
29- I copy and paste the file, image, and logo that we want to be displayed on the splash screen in the drawable folder.
Note:
It should be noted that you can import the file outside the Android Studio environment and use it. We use the android: src feature to the route to do this.
Note:
The name of the image we want in this tutorial is android_logo.png.
The code that must be entered in activity_splash.xml to insert the image into the project is as follows:
30- We will implement the project.
31- It is possible to see a toolbar in splash screens, which is better to remove because no toolbar is shown in splash screens.
32- To do this, we need to define a theme without a toolbar.
33- Then, we can put the theme property inside the tag. Like the following:
34- The activity tag is as follows:
35- Then, run the project again.
36- After running, we will see that the splash screen is displayed for 3 seconds and then transferred to the application's main page. It also has no toolbar and does not return to the start screen by pressing the back button and closing the application altogether.
37- In the next steps, we want to add an audio file to the start screen so that when launching the application, we will first see the start screen, which is accompanied by music.
38- To add an audio file to Android files, we must first create a folder called raw inside the res folder.
39- We have to copy the audio file we want in the raw folder. Note that all audio file formats are supported.
40- First, we create an object from the MediaPlayer class called splash Sound. Like the following:
41- If we run the project, we will see that by running the application, the sound screen will also run at the same time. Of course, you should ensure that the duration of the audio file you select is not more than the time you have set aside for displaying the start screen.
The final and complete code that should be included in SplashActivity.java is as follows:
In this part of the tutorial, you also learned how to create a page and use it for a start screen.
We knew that the start screen is a page displayed before running the application, which can include a logo or custom designs. You can also insert an audio file to play music while displaying the startup screen for a certain period before running the application.