Android: Splash screen, creating an activity from another activity

19 Mar 2012

In today's post I will explain how you can launch an activity from another activity. This allows us to create a splashscreen with the branding or other info before loading our real application. I will be using the application I made in my previous post Introduction to Android application development. The complete project used here is available at hotfile.

MD5: 82a173844c19228ae80286018ff740e1
SHA1: b380225d944918d5a6ce9b4f496ec3ae6384c8dd

Creating the Splashscreen activity

We will create our splashscreen activity. Create a new class called Cloud101Splashscreen in the com.cloud101 package:

package com.cloud101;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Cloud101Splashscreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    startActivity(new Intent(
                            "com.cloud101.Cloud101CalculatorActivity"));
                            finish();
                }
            }
        };
        splashTread.start();
    }
}

What we are doing here is extending Activity and creating a thread that will make our application show the splashscreen before launching the calculator activity. We set the layout to splash. Splash is a new XML layout file we will create in the layout folder. After this we create a thread that will launch our main activity by creating a new Intent, after a waiting period of 2 seconds.

sleep(2000);

2000 is the amount of miliseconds the thread will sleep, so this means that after 2 seconds of showing the splashscreen a new activity will be started.

finish();

This will finish the activity and close it.

Layout of the splashscreen

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
      <ImageView
          android:id="@+id/ImageView01"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center"
          android:src="@drawable/cloud1012"
          android:background="#ffffff"
          android:scaleType="centerInside">
      </ImageView>
</RelativeLayout>

Some notes:

  • android:layout_gravity="center"
    

    This centers the image in the middle of the display

  • android:background="#ffffff" 
    

    This sets the background to white

  • android:scaleType="centerInside"
    

    Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

  • android:src="@drawable/cloud1012"
    

    I made an image called cloud1012.png and put it in the drawable folder. You can use your own png's or copy the one from the package.

Changing the manifest

We are almost finished, we just need to change the manifest, this is what my manifest looks like now:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cloud101"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Cloud101Splashscreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Cloud101CalculatorActivity" >
            <intent-filter>
                <action android:name="com.cloud101.Cloud101CalculatorActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The first activity we launch is the splashscreen activity, because it will automatically load the calculator activity when the thread finishes sleeping. You can now run the application and it will first show you the splashscreen before going to the calculator.

Final Word

I hope you enjoyed the tutorial, if you have any problems or if you notice a mistake, please post them in the comments.