How to create android splash screen

In this tutorial I’m going to show you how to easily create an android splash screen in your application.

The whole idea of this sample implementation is to start and display an activity for a certain amount of time and then start new activity. Our activity code is listed below.

SplashActivity.java

package com.itcuties.tutorial.android;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
 
public class SplashActivity extends Activity {
 
   private static String TAG = SplashActivity.class.getName();
   private static long SLEEP_TIME = 5;    // Sleep for some time
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
 
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
      this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    // Removes notification bar
 
      setContentView(R.layout.splash);
 
      // Start timer and launch main activity
      IntentLauncher launcher = new IntentLauncher();
      launcher.start();
   }
 
   private class IntentLauncher extends Thread {
      @Override
      /**
       * Sleep for some time and than start new activity.
       */
      public void run() {
         try {
            // Sleeping
            Thread.sleep(SLEEP_TIME*1000);
         } catch (Exception e) {
            Log.e(TAG, e.getMessage());
         }
 
         // Start main activity
         Intent intent = new Intent(SplashActivity.this, MainActivity.class);
         SplashActivity.this.startActivity(intent);
         SplashActivity.this.finish();
      }
   }
}

 

As you see the implementation is really simple. We have a private class which is responsible for waiting some amount of time and launching a main activity in a new thread. We construct and start this thread at the end of onCreate method when a view has been set.

Remember that SplashActivity is the starting activity of our application so don’t forget to set android.intent.category.LAUNCHER category in Your AndroidManifest.xml file. Our sample manifest file code looks like this

AndroidManifest.xml

< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
          package = "com.itcuties.tutorial.android"
          android:versionCode = "1"
          android:versionName = "1.0" >
 
    < uses-sdk
       android:minSdkVersion = "7"
       android:targetSdkVersion = "7" />
 
    < application
       android:icon = "@drawable/ic_launcher"
       android:label = "@string/app_name"
       android:theme = "@style/AppTheme" >
       < activity
          android:name = ".SplashActivity" >
          < intent-filter >
             < action android:name = "android.intent.action.MAIN" />
             < category android:name = "android.intent.category.LAUNCHER" />
          </ intent-filter >
       </ activity >
       < activity
          android:name = ".MainActivity"
          android:label = "@string/title_activity_main" >
          < intent-filter >
             < action android:name = "android.intent.action.MAIN" />
          </ intent-filter >
       </ activity >
    </ application >
 
</ manifest >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值