Implementing a Splash Screen in Android: Full Guide

DeanDjermanovi1 98 views 81 slides Oct 06, 2022
Slide 1
Slide 1 of 81
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81

About This Presentation

Splash screen is one of the most vital screens in the application. It's the user's first experience with the application. Android 12 introduced splash screens as default behaviour along with the SplashScreen API. With these changes, it’s a good time to take a deep dive into splash screen i...


Slide Content

Implementing a
Splash Screen in
Android: Full Guide
October 6th, 2022

First Impressions Matter
With any introduction – to people, places, or products – first impressions are critical
It only takes 50ms for users to form a first impression
It’s vital that your first impression is always your best one

1
What is a Splash Screen?

“The launch screen is a user’s first experience of your app.”

Material Design

What is a Splash Screen?
The very first screen the user sees when they open up an app on a mobile device
It appears while the app is loading when the user has just opened up the app
Splash screen is often called a launch screen
Mostly has a logo, brand name, or slogan related to the product and it is minimalistic in
nature

1.1
App Startup

??????

System Process
time
Load & Launch
Application
Application Process
Display start
window
<other stuff>
MainThread
Application
onCreate
Activity init
Activity
onCreate
Initial composition, etc.
Swap start
window for
app

App startup states
App launch can take place in one of three states
Each state affects how long it takes for your app to become visible to the user
Startup states:
•Cold start
•Warm start
•Hot start

Cold Start

App is starting from scratch
Happens when your app’s being
launched for the first time
Perfect for showing a splash
screen that gives the impression of
a shorter delay

3
Warm Start

The system does not need to
initialize everything but has lost
some information and needs to
reload it

Doesn’t take as long as a cold
start, but it’s often long enough to
warrant a splash screen
2
Hot Start

All the system does is bring your
activity to the foreground

It’s quick, leaving no time for a
splash screen
1

1.2
A History of Splash Screens

Splash Screens Over the Years
Once viewed as an anti-pattern
Often misused as a way to embed branding into apps
Intentionally delaying users from reaching the content for a few seconds
As Android evolved and app requirements grew, the time taken to launch apps began to
increase
The Material Design guidelines started recommending a splash screen
Lack of documentation → multiple approaches of adding a splash screen
No APIs or libraries existed to ensure consistency across codebases

Android 12 (API level 31)
Brought a new SplashScreen API
All apps show a default splash screen
Backwards compatible (to a certain extent)

Idea behind a new SplashScreen API
Opinionated implementation
Standard and consistent implementation across all devices and operating systems
No boilerplate code
Provide branding and animation support

2
Let's make a splash! ??????

API 31+ (Android 12+) Prior API 31

Platform API vs Compat Library
Provides control over the splash screen once
the application is started
On API 31+ (Android 12+) this class calls the
platform methods
Prior API 31, the platform behavior is
replicated
SplashScreen compat library - wraps the
SplashScreen API
SplashScreen
ImplImpl31

Add Dependency

Customizable
elements of a
splash screen
Customizing
the animation
for
dismissing the
splash screen

Keeping it
on-screen for
a longer period
Appearance

Appearance
Icon
Background
Branding logo
Icon Background

2.1
Icon

Icon
Icon can be any vector drawable or PNG
By default, SplashScreen uses the launcher icon
Icon

Icon Rules
Needs to follow the same rules as the Adaptive
Icon - needs to fit within a circle whose
diameter is 2/3 the size of the icon

Icon Mask
Icon is masked using system mask
Don’t use adaptive icon - adaptive icon is
already masked (cropped two times)
Use just the foreground part and set the
background separately

Icon Size
With an icon background: 240×240 dp
Without an icon background: 288×288 dp
The actual values don't really matter if you use a
vector icon
192dp
288dp 240dp
160dp

Theme.SplashScreen
Defines Splash Screen
attributes common to all
versions
Theme.SplashScreen.IconBackground
Should be used if you wish to
display a background under
your icon
Themes

Define splash screen style

This style will define your splash screen
Define splash icon

Set the icon for the Splash screen
Define post splash
screen theme

Defines which theme to apply to the
Activity once the splash screen is
dismissed
Installing the splash
screen
Set the splash style

Set the `theme` attribute to splash
screen style that you’ve defined
05
01
02 03
04

Define splash screen style
Setting style attributes changes splash screen
appearance
Attributes that start with “android:<name>” are
only for API31+

Define splash icon
windowSplashScreenAnimatedIcon attribute
sets the icon for the Splash screen
Despite the name, it doesn’t need to be
animated
On API31+, this can be an animated icon

Define post splash screen theme
postSplashScreenTheme attribute defines which
theme to apply to the Activity once the splash
screen is dismissed

Setting the splash style
In manifest file, set the `theme` attribute of
the whole <application> or just the starting
<activity> to splash screen style that you’ve
defined

Installing the splash screen
In the `onCreate` method of the starting activity, call installSplashScreen to install the splash
screen
installSplashScreen method creates a SplashScreen instance associated with that Activity and
handles setting the theme to [R.attr.postSplashScreenTheme]
Needs to be called before Activity.setContentView or other view operations on the root view (e.g
setting flags)

Installing the splash screen
SplashScreen reference is assigned to a val to use
later for extra customization

API 31+ (Android 12+) Prior API 31

API 31+ (Android 12+) Prior API 31

Debug group

App Launch - Known Issue*
SplashScreen API on Android 12 shows blank screen when launched via third-party-launchers,
app-info, and from IDE
By default splash screen isn’t shown - launcher sets a special flag that shows the splash screen
Splash screen not displayed if you open an app from deeplink
Android Studio runs app using adb which is considered as a launch from another app, not a
launcher

2.2
Background

Splash background
Background color of the splash screen
windowSplashScreenBackground attribute
Defaults to the theme's windowBackground
Single opaque color
Background

Adding splash background

2.3
Icon Background

Icon background
Optional color displayed below the icon
Useful if you need more contrast between the
icon and the window background
Icon Background

Change theme
To enable the icon background, splash theme
needs a different parent
Ensures that the scale and masking of the icon
are similar to the Android 12 Splash Screen

Adding an Icon background
windowSplashScreenIconBackgroundColor
attribute sets a background behind the splash
screen icon

API 31+ (Android 12+) Prior API 31

2.4
Branding Image

Branding image
Optionally, you can set an image to be shown at
the bottom of the splash screen
The design guidelines recommend against
using a branding image
Only supported on API 31+
Should have the size of 200×80 dp
Use vector asset to prevent loss of quality when
the image is scaled
Branding logo

Creating a New Style for Android
12
Create a new resource directory with the API
31 qualifier
Add a copy of the theme you declared for your
splash screen
android:windowSplashScreenBrandingImage
attribute places a drawable image in the bottom of
the starting window

API 31+ (Android 12+) Prior API 31

Keep aspect ratio
Centering vector drawable in a layer-list
preserves aspect ratio

API 31+ (Android 12+) Prior API 31

3
Animate Splash Screen

1.
Enter animation
Consists of the system
view to the splash screen
This is controlled by the
system and is not
customizable
Splash screen
Allows you to supply your
own logo animation

2.
Exit animation
Animation run that hides
the splash screen
Can be customized
3.
Splash Screen animation launch sequence

3.1
Animated Icon

Animated Icon
Has aesthetic appeal
Provides a more premium experience
User research shows that perceived startup time is less when viewing an animation
Only supported on API 31+

Adding animated icon
windowSplashScreenAnimatedIcon attribute
will play the animation while showing the
starting window, if you supply an animated
vector drawable

Adding animated icon
windowSplashScreenAnimationDuration
attribute sets the duration, in milliseconds, of the
window splash screen icon. Recommend not to
exceed 1,000 ms on phones
It does not actually affect the duration of an
animation of the icon as this is set by an
AnimatedVectorDrawable

API 31+ (Android 12+) Prior API 31

3.2
Exit Animation

Exit Animation
You can customize the exit animation of the splash screen that is run once the app has loaded
To customize it, you'll have access to the SplashScreenView and its icon and can run any
animation on them
The splash screen needs to be manually removed when the animation is done
Backwards compatible

OnExitAnimationListener
onSplashScreenExit
SplashScreenViewProvider
The listener will be called once
the splash screen is ready to be
removed
Callback called when the splash
screen is ready to be dismissed
The caller must remove the
splash screen once it's done
with it
An object holding a reference
to the displayed splash screen
Contains time information
about the animated icon
Provides references to the
splash screen view and icon
view

Adding Exit Animation
Get references to the splash screen view and
icon view
splashScreenView is used to create custom
animation from the splash screen to the
application
iconView contains the splash screen icon as
defined by windowSplashScreenAnimatedIcon
attribute

Define and Play Exit Animation
Define and play animation for splash screen
view and icon
Once the animation is finished, remove the
splash screen from view hierarchy
SplashScreenViewProvider.remove() always
needs to be called when an
OnExitAnimationListener is set

API 31+ (Android 12+) Prior API 31

3.3
Common Requirements

Lottie animation
Not possible to use directly with the Splash Screen API - there’s a workaround

Take the first frame of the
animation and convert it to
an SVG
Import and convert the
SVG into a vector drawable
using the Resource Manager
tool
Add lottie animation to the
initial Activity and ensure
that LottieAnimationView is
aligned with splash icon
Synchronize the end of the
splash screen animation
with the start of the lottie
animation and manually
dismiss the system splash
screen to seamlessly transition
from it to the initial activity

Shared element transition animation
Not possible to use directly with the splash screen
Workaround is similar to lottie animation
Add element to the initial Activity and ensure that the element is aligned with element on splash
icon
Synchronize the end of the splash screen with the start of the element animation and
manually dismiss the system splash screen to seamlessly transition from it to the initial activity

The disclaimer
Don't do that. It's bad
Letting users wait on a fancy but useless animation is against good UX
Application start should be as fast as possible and the splash screen should be dismissed as soon as
you have some content to display

4
Legacy Methods

Launcher theme
During the cold start, the window manager tries to
draw a placeholder UI using attributes from the
app theme
Default windowBackground can be changed
Splash screen only shows when needed - no
intentional delays
Inflexible - no animations
Splash Activity
Dedicated splash screen Activity that shows up for
fixed amount of seconds
More flexibility
Introducing additional delay
Your app has two splash screens on Android 12
and above

??????

Launcher System Splash
Screen
Custom Splash
Screen
Initial Screen

Splash Activity

Splash Activity
If targetSdk is 31 or above, Android Studio
issues a warning if you have custom splash
screen implementation
Migrate your app to the SplashScreen API

5
Summary

Summary
Splash screen is a critical aspect of the user experience
How to use the SplashScreen API to provide a smooth experience for users
Test your existing projects to ensure they’re compatible with Android 12

Show me the code
https://github.com/dean95/splash-screen-sample

Thank You!
Dean Djermanović
Android Engineer
dean_95_
deandjermanovic