A splash screen is basically the loading screen of the app. Some people use the splash screen to load the application while others use it to show branding. Today in this article we will be looking at how to create a simple splash screen which will be shown to the user's for a couple of minutes before the app is loaded.
I will be using a simple XML and Java class to demonstrate the same, so let's see how we can create our Splash screen :
SplashScreenActivity.java
The corresponding XML file for the UI :
splashscreen.xml
That's it, its that simple to display a Splash Screen. The code is self explanatory with the comments but here is a gist of what we did above:
1. The XML for the UI just contains an ImageView which shows the splash screen image.
2. We created a new handler and basically made it wait for the required delay and then started the Login Activity(This is the activity you want to open after the splash screen)
3. The more tricky part here in this tutorial is the code in the onPause(), we call this.finish() in the onPause. This is done so that we kill this activity once the splash screen is shown, because we don't want to be showing the splash screen image again if the user presses the back button.
A better approach instead of calling finish() would be to set the android:noHistory="true" attribute for SplashScreenActivity in the AndroidManifest. Also make sure you set SplashScreenActivity.java as your Launcher activity so that when the user starts the app the splash screen is shown first.
Well, that's all there is from my end for this tutorial. If you have any queries feel free to drop me a comment below.
This is the image I have chosen to display as my splash screen for my app. You can select any image you wish as your splash screen. Android doesn't provide any built in mechanism for displaying a splash screen, so we will be using a timer to display the image for a certain time period before we start the app.
I will be using a simple XML and Java class to demonstrate the same, so let's see how we can create our Splash screen :
SplashScreenActivity.java
public class SplashScreenActivity extends Activity { // Show the Splash Screen for 3secs(3000ms) long START_UP_DELAY = 3000L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); // Add delay so that Splash Screen is displayed for 3secs new Handler().postDelayed(new Runnable() { @Override public void run() { Intent loginPage = new Intent(SplashScreenActivity.this, LoginActivity.class); startActivity(loginPage); } }, START_UP_DELAY); } // Override the onPause method to kill this Activity, so that pressing the back button doesn't bring up this screen. @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); this.finish(); } }
The corresponding XML file for the UI :
splashscreen.xml
That's it, its that simple to display a Splash Screen. The code is self explanatory with the comments but here is a gist of what we did above:
1. The XML for the UI just contains an ImageView which shows the splash screen image.
2. We created a new handler and basically made it wait for the required delay and then started the Login Activity(This is the activity you want to open after the splash screen)
3. The more tricky part here in this tutorial is the code in the onPause(), we call this.finish() in the onPause. This is done so that we kill this activity once the splash screen is shown, because we don't want to be showing the splash screen image again if the user presses the back button.
A better approach instead of calling finish() would be to set the android:noHistory="true" attribute for SplashScreenActivity in the AndroidManifest. Also make sure you set SplashScreenActivity.java as your Launcher activity so that when the user starts the app the splash screen is shown first.
Well, that's all there is from my end for this tutorial. If you have any queries feel free to drop me a comment below.
No comments:
Post a Comment