Skip to content

Simple Android app explaining the right application of a splash screen (or opening screen)

License

Notifications You must be signed in to change notification settings

davicoradini/Android-splash-screen-the-right-way

Repository files navigation

How to use a Splash screen correctly

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

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
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

<?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

<?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

<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.

About

Simple Android app explaining the right application of a splash screen (or opening screen)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages