How to make a Splash Screen in Android with Android Studio - Letecode
Suivez nous
@johnmbiya - 09 septembre 2020 2212 dans Tutoriels Android

How to make a Splash Screen in Android with Android Studio

Learn How to make a start Activity of your App in just some simple steps

How to make a Splash Screen in Android with Android Studio

Hello guys, in this tutorial we will talk about "Splash Screen" on Android and have a closer look on how to implement it in a very simpliest way.

So, A splash screen is the first screen on most mobile Apps when it is opened. it's used to display some basic information about the app, like a logo, company name, etc just before it loads completely.

To continue with this tutorial, make sure you have basic knowledge using Android Studio and Also Java language.

Let get Started 😁 

First start a new Android Project by going to File > New project or just tap the Create New Project button on the start page of your Android Studio.

Choose Empty Activity as your project template, then click next.

Give any name to your project, in my case i just set it to Splash Screen for this tutorial. and then click finish and wait until the project build successfuly.

To make this project more fun, we will create two activities, one for the splash and the other one for a MainActivity where the App should go after displaying the splash sreen.

So as you created your project successfuly, Android Studio generate automaticaly a MainActivity with its layout containing only a TextView that displays "Hello World" in the center of the screen. we don't have to care about that, but in my case i will just change the Text to "Welcome home".


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome Home !"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Now, let create our Splash Activity with a logo in the center of the screen.

Go to File > New > Activity > Empty Activity and give your Activity a Name like mine SplashActivity, you can give it any name, but just to be more simple.

Enable Launcher Activity to make this as the starting Activity of our App, and click finish

So we need to fix somethings in AndroidManifest.xml file, to have only one Launcher Activity


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SplashScreen">
<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">

</activity>
</application>

it must look like this.

One more thing is to have an App Theme that don't have an ActionBar, change this line in your style.xml or themes.xml ( AS 4.1 )

with Materia Design ( AS 4.1 )


<style
name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">

without material design :


<style
name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Now let design our Splash Activity

Copy paste your logo image in res/drawable as png file, and use it like below in a ConstraintLayout.


<ImageView
android:id="@+id/imageView"
android:src="@drawable/logo_letecode"
android:layout_width="120dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Your logo will be constraint in the center of the screen.

Final step is to add Logic in our SplashActivity.java so make our screen wait for few seconds and launch automaticaly the Welcome Screen.

add this lines of codes in the onCreate() method


Thread splashScreenStarter = new Thread() {
public void run() {
try {
int delay = 0;
while (delay < 2000) {
sleep(150);
delay = delay + 100;
}
startActivity(new Intent(SplashActivity.this, MainActivity.class));

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}

};
splashScreenStarter.start();

The above code just use a Thread that can handle a runnable Task while sleep each 150 milliseconds ( 1.5 s), 100 times and launch the MainActivity, when finally it will finish the splashActivity so that the user can't return on this Screen when he clicks the back button.

An other alternative way is to use Handler.postDelayed


new Handler()
.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, 1500);

Now run and test, enjoy ! 

if you found any issue, let me know with a comment and don't forget to subscribe and share this tutorial.

Find full code on GitHub here.

@johnmbiya
Jean Claude Mbiya (johnmbiya)

je suis developper fullstack, Dévelopeur android (AAD), créateur de letecode.com, bloggeur, formateur, fondateur de Guide Light System, j'aime la science informatique 😎👨‍💻

P : 386 7 7 24
Abonnez vous !

Recevez les dernières nouvelles de letecode directement dans votre boîte de réception. Réjoignez +500 Abonnés, Nous n'envoyons pas des spam !

Voir plus

2 commentaire (s)

  • Image placeholder
    Louis CAD il y a 1 an

    Please, never delay your app start to show a splash screen. Just let users do what they need ASAP and let them get back to their life ASAP. Also, use coroutines, avoid creating new threads and calling Thread.sleep.

    Répondre
    Réponses
    • Image placeholder
      @johnmbiya il y a 1 an

      Thank you for your Feedback @Louis , as this article is for beginners will update soon and exemple using coroutines (KOTLIN) and more alternative ways.

Laissez votre commentaire à @johnmbiya