We are going to begin the application with a presentation screen, called splash screen. This screen will allow us afterward to pre-load a lot of resources used during the application. This presentation screen will not appear when you will click the previous button, it will leave simply the application without re-posting this screen.

Thus we are going to show an image during a few moments before showing the main menu of the game.

We are going to resume(to take back) the tutorial of barebonescoder at the address : http://www.barebonescoder.com/2010/04/a-simple-android-splash-screen/ ) .

We begin by creating the “splashscreen.xml” file which will describe what must be shown on our screen. For that purpose, on the layout item of our project, with the right button, we make new / file to add a XML file which we name splashscreen.xml. In this file, we add the following code:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center_horizontal|center_vertical" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/spacoid"> <TextView android:layout_height="wrap_content" android:text="LOADING" android:id="@+id/textView1" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"/> </LinearLayout>

With this code, we add a LinearLayout in which we position in background our presentation image "@drawable / spacoid".

We are going to update the file AndroidManifest, to define correctly the screen of our application.

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

     package="com.jeux.splashscreen"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:label="@string/app_name"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="portrait" android:name="SplahScreenActivity">

           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
   </application>

</manifest>

Il ne reste plus qu’à ajouter le code permettant l’affichage de cet écran de présentation avec le code suivant :

With the following lines:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="portrait"

we force the display in portrait mode, as well as the display in full screen by eliminating the status bar at the top of the screen.

It remains to add the code allowing the display of this presentation screen with the following code:

package com.jeux.splashscreen;

import android.app.Activity; import android.os.Bundle;

public class SplahScreenActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate( Bundle savedInstanceState ) { // Must be first instruction of onCreate. super.onCreate( savedInstanceState ); // Show Splash screen. setContentView( R.layout.splashscreen ); // Create and run thread. Thread splashThread = new Thread() { @Override public void run() { try { int waited = 0;

while( waited < 5000 ) { sleep( 50 ); waited += 100; } } catch( InterruptedException e ) { e.printStackTrace(); } finally { finish(); } } }; splashThread.start(); } }

After execution, we obtain the following display, during some seconds, then the application ends:



01_-_SplashScreen.jpg

Download corresponding archive.