Home Android splash screen - The right way
Post
Cancel

Android splash screen - The right way


Repost from https://github.com/davicoradini/Android-splash-screen-the-right-way.


How to use a Splash screen correctly

Well, there are a lot of blog tutorials teaching how to create a splash screen like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SplashActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        ...
        
        new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(this, MainActivity.class));
                    }
                }, 3000); //fu***ng 3s delay
    }
}

The thing is, this is nothing but a 3 seconds waisting of the user’s time!

As you can notice, Google has gotten their opinion in favor of Splash Screens on their Official Material Design Documentation
But, is this something you just put anyway on your app to make the user waste his time?
No. And Google advocated against splash screens like this, and even called it an anti-pattern on this video.
So, is there a way to make use of this pattern on the right way? The answer is, Yes!

So, how could one do to create a Splash Screen just for the amount of time the App needs to open the Main Activity?

Well, actually, it’s easy. The ingredients are:
  • An Activity for the Splash Screen (without the layout file)
  • Your Manifest: to declare you Splash Screen as the Launcher
  • One drawable file to customize the splash screen a little

The splash view has to be ready immediately, even before you can inflate a layout file in your splash activity.

The recipe:

Create your Splash Screen Activity
1
2
3
4
5
6
7
8
9
10
11
12
13
public class SplashActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /** START - this is the purpose of this Activity */
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
        /** END - everything more than this is time consuming */
    }
}

activity_splash.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/colorGrey"/>

    <item>
        <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

and your Manifest should look something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andyfriends.showcase">

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.SplashActivity"
            android:label="@string/app_name"
            android:theme="@style/SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" />
    </application>

</manifest>

we’ve set a different Theme for the SplashActivity, so we can call our drawable resource on it

1
2
3
<style name="SplashScreen" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/activity_splash</item>
</style>

That is it. You can clone the project using Android Studio and take a look.

This post is licensed under CC BY 4.0 by the author.