An Android Application in incomplete without a Splash Screen and when it comes to supporting multiple devices, it can be a pain to develop these images. A tried and tested way to develop images for multiple devices is to use 9 Patch Imaging. I personally found 9 Patch images pretty difficult to understand. So, I tried using different ways which could help me make a splash screen for my Android application and I ended up doing this simple thing that allowed me to make my splash screen work on various screen sizes and resolutions. This is a simple tutorial, which will enable you to make a splash screen for your Android Application without using 9 Patch images. Before you begin with the tutorial, make sure you have a project in Eclipse that you want to create a Splash Screen for. Next, you must design an image for the splash screen of your application. Here are the specifications for this image. You can make your splash screen in Photoshop, or use the free image creation Application GIMP.
- Background: Transparent
- Format: PNG
- Size: 500 px X 500 px
After you have the above things, proceed with the tutorial below.
Android Splash Screen for Multiple Screen Sizes
- Go To New -> Android XML File and name the file “splash.xml“. Place it in the Layout Folder in your project, choose “RelativeLayout” under root element and click on Finish.
- Now, name the file you created earlier as “splash_sc.png“. Now, Drag and drop that image into your drawable folder. If you have drawable folders for different device sizes (like hdpi, mdpi etc.) then place the same file in all of them.
- Now go to the XML view on your splash.xml file and add the following code to the Relative Layout Tag in there.
android:background="#000000" android:orientation="vertical"
- Now, Add the following code between the Relative Layout tags.
<ImageView android:id="@+id/imageView1" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" android:contentDescription="Splash Screen Logo" android:scaleType="fitCenter" android:src="@drawable/splash_sc" />
- This completes our layout part. Now let us do the coding part in Java. To do that, create a new Java class with the following settings.
- Next, in there, delete all the Java code that is in there and add the following code.
package com.slashcoding.helloworld; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.WindowManager; public class SplashScreen extends Activity{ @SuppressWarnings("rawtypes") Class Aneesh; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.splash); Thread timer = new Thread() { public void run() { try { sleep(3000); // 3000ms is the time to sleep } catch (InterruptedException e) { e.printStackTrace(); } finally{ try { Aneesh = Class.forName("com.slashcoding.helloworld.MainActivity"); Intent intent = new Intent(SplashScreen.this,Aneesh); startActivity(intent); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; timer.start(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); finish(); } }
- Save the file. Now open the AndroidManifest.xml file from your project and click on the XML view. It should look something like this.
- Now, add the following code to the file inside the application tag.
<activity android:name="com.slashcoding.helloworld.SplashScreen" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
- Now change the code shown in the below image to this.
<activity android:name="com.slashcoding.helloworld.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.START" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- Well, that’s all. Now just run the application and you should see the desired result!
I hope you enjoyed this tutorial and this tutorial helps you create a Splash Screen for your Android Application in the future. Get back to me for anything if you may have any questions. Use the comments section below or use the contact form. Also, stay subscribed to Slash Coding via RSS Feeds, or Liking our Facebook Page, or by Following us on Twitter. See you Around! 😉