-
30nov
Développez un splash screen pour vos applications
Ce tutoriel est réalisé et proposé par bunjix, développeur d’application sous Android. Vous pouvez voir son blog sur http://bunjix.fr.
Un splash screen est un écran que l’on place au lancement de l’application et qui dans la plupart des cas permet de faire des traitements de type chargement de donnée, ou tout simplement affichage tel que le nom de l’éditeur avec son logo etc.
Pour réaliser ce tutoriel j’ai donc crée un simple écran comme celui-ci :

Pour un splash screen nous avons donc au minimun 2 écrans et donc 2 activity.
Le premier est celui du splash screen.<?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"> <TextView android:id="@+id/TextView01" android:textSize="30dip" android:text="@string/splash_text" android:gravity="center_vertical|center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <TextView android:id="@+id/TextView02" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_marginBottom="20dip" android:autoLink="web" android:text="@string/bunjix" android:textSize="20dip" android:layout_width="fill_parent" android:gravity="center" /> </RelativeLayout>L’activity qui va avec :
public class SplashScreen extends Activity { private static final int STOPSPLASH = 0; private static final long SPLASHTIME = 5000; private Handler splashHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case STOPSPLASH: //remove SplashScreen from view Intent intent = new Intent(SplashScreen.this, MainActivity.class); startActivity(intent); break; } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); Message msg = new Message(); msg.what = STOPSPLASH; splashHandler.sendMessageDelayed(msg, SPLASHTIME); } }Dans cette classe vous pouvez voir l’instanciation d’un Handler, élément principale du fonctionnement.
Explication du code.
Comme d’habitude la fonction onCreate appelle super.onCreate puis affiche notre layout. Ensuite on créer un Message avec une simple information (le message).
On appel ensuite la fonction sendMessageDelayed de notre Handler.Un handler c’est quoi ? C’est une classe qui va se charger d’exécuter un traitement demandé par un message.
Dans notre code, avec la ligne splashHandler.sendMessageDelayed(msg,SPLASHTIME);, on envoie en décalage de 5 secondes (SPLASHTIME = 5000 millisecondes, soit 5 secondes) le message msg.Lorsque le handler reçoit le message msg qui contient la demande d’arret (msg.what = STOPSPLASH ), la fonction handleMessage du Handler est appelé, on passe donc à l’activité MainActivity. Cette activité est l’activité d’entré de votre application.
Dans le manifest il faut maintenant changer l’activité d’entrée de l’application pour que ce soit le splash screen :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bunjix.tuto.SplashScreen" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".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> <activity android:name="MainActivity"></activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
Vidéo de formation pour Android
3 commentaires
Laissez un commentaire
Vous devez être identifié pour publier un commentaire.
Connexion - Inscription
S'inscrire










Juste un bémol : avec code, lorsque l’activité principale se termine, on revient sur le splash screen. L’ajout de “finish();” avec le “startActivity” permet de terminer le splash screen, ainsi lorsque l’activité principale se termine, on revient bien sur le Home screen. Autrement merci pour le tuto !