Using a theme to show a splash or placeholder while activity loads

Rule: never ever implement a splash screen using a separate activity. Splash screens should be used to show something while the app loads, and is not a way to delay app launch just to show a logo.

This snippet shows how to do add a loading screen the correct way by creating a special theme (which may contain a logo or (preferably) a placeholder for interface elements). Once the activity is loaded, the theme is replaced by the final one.

Adjust your Activity

Put this in the onCreate of the Activity you’re launching. It removes the background from the theme to prevent overdraw:

getWindow().setBackgroundDrawable(null);

Create a placeholder drawable

This drawable will be shown while the app loads. This example shows placeholders for the status bar and toolbar, as well as an icon in the center of the screen.

Save it as bg_loading.xml in the resources/drawables/ folder.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Background color -->
    <item>
        <color android:color="@color/window_background"/>
    </item>

    <!-- Fake placeholder for the status bar -->
    <item
        android:height="25dp"
        android:gravity="top">
        <color android:color="@color/statusbar_bg"/>
    </item>

    <!-- Fake placeholder for the Toolbar -->
    <item
        android:height="?actionBarSize"
        android:gravity="top"
        android:top="25dp">
        <color android:color="@color/toolbar_bg"/>
    </item>

    <!-- Loading screen logo -->
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/pixplicity"/>
    </item>

</layer-list>

Create a special theme

Add this to your themes.xml (or styles.xml).

    <!-- Set this theme on the launching activity, through the manifest -->
    <style name="AppTheme.NoActionBar.Loading">
        <item name="android:windowBackground">@drawable/bg_loading</item>
    </style>

And finally don’t forget to apply the theme to your activity in the manifest.