Skip to content

Instantly share code, notes, and snippets.

@pokk
Created May 7, 2017 05:56
Show Gist options
  • Save pokk/0e6b4ef05bf557fabdd556c0f8bb8a5d to your computer and use it in GitHub Desktop.
Save pokk/0e6b4ef05bf557fabdd556c0f8bb8a5d to your computer and use it in GitHub Desktop.
Splash View for avoiding the cold start app

[TOC]

Introduction

The app will show white screen or black screen each time when you cold start the app. For UX or UI, this is not good user experenice.

For that, we will fix knid of small problem with the splash view.

How to do

First step

Create a drawable view in your drawable folder, named splash_view.xml.

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="?colorPrimary"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/YOUR_PIC"/>
    </item>
</layer-list>

Declare the style to your styles file.

<style name="AppTheme.Splash">
    <item name="android:windowBackground">@drawable/splash_view</item>
</style>

Second step

Create an activity for the splash view. You can customize how long to show the splash view.

class SplashActivity: Activity() {

    companion object {
        private val SPLASH_DELAY_TIME: Long = 500
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Delay 0.5 second for showing the splash view.
        Handler().postDelayed({
            this.let {
                // Transfer to your main activity after delay 0.5 second.
                it.startActivity(Intent(this, MainActivity::class.java))
                it.finish()
            }
        }, SPLASH_DELAY_TIME)
    }
}

Third step

Set the splash view in your AndroidManifests.xml.

<application
    android:name="myapp.myapp"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <!-- Your theme here!!! -->
    <activity
        android:name=".ui.activities.SplashActivity"
        android:theme="@style/AppTheme.Splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name="myapp.myapp.SecondActivity"></activity>
    <activity android:name="myapp.myapp.ThirdActivity"></activity>
</application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment