How to make a splash screen like Youtube on Android?
The reason why I’m writing this post is a lot of people don't know how to create a splash screen. I’ve seen people creating a separate activity and fragments, I have seen people even creating views and hiding them after a few seconds.
Let's start with the basics. What is a Splash Screen?
A splash screen is a simple minimalistic screen containing only a logo and the app version if you want to be a nerd. This special screen appears only while launching an app or a game. Usually to avoid that annoying blank screen while the app loads. Trust me no one wants to see a blank screen while launching an app.
Now that we know what a splash screen is! let's make one and what better way to do this than implementing something similar to the Youtube apps splash screen.
This can be achieved easily with a single activity and without having to toggle the views and no need for separate splash screen activity to achieve this.
Enough talk! where is the code?
Let's start by creating a drawable which will act as a logo for our app. I’m going to call this
ic_splash_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_logo" />
</item>
</layer-list>
this will serve as the background of the activity while it is loaded. The bitmap will just contain your logo. It could be a png, jpg or any other image file.
Next, you will need to create a style for your launcher activity which will show the logo while the activity loads. So head over to your styles.xml and create a new style that extends the AppTheme.
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/ic_splash_background</item>
</style>
once you have the style all you need to do is apply it to the launcher activity likewise.
<activity
android:name=".MainActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Final step just set a background colour to your launcher activities layout and you are all set.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorActivityBackground"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And that my friend is how you create a proper splash screen in android. So now when you launch your app it will show a nice little logo before it starts while the app loads instead of the plain empty screen.
You can check the sample code here