[Java] 4 steps to implement splash screen on Android

Hello, this is momorito. This time, I will summarize the ** procedure for implementing the splash screen ** when creating an Android application.

Possible ways to implement a splash screen

Etc., but this time we will implement it with the former ** Theme replacement type **.

1 Introduction

(1) Completed form

As shown in the gif below, tap the app → display the splash screen → after a few seconds, apply the original theme and aim to display the main activity. qiita-square

(2) Rough image of splash screen display

  1. Create a Theme to display the splash screen and apply it to the Activity at startup (MainActivity this time).
  2. Start MainActivity and wait for a few seconds (2 seconds this time) with onCreate.
  3. Apply the original Theme to MainActivity.

(3) Prerequisites

2 Mounting procedure

(1) Create a theme (splash.xml) for the splash screen

Prepare the image you want to display

This time I used the image below. infoDeviceSplash.png It is a png prepared by 160px x 160px. (Other sizes are OK as long as they are displayed.) The file name is splash_logo.png.

Set the background color for the splash screen.

Since the image is displayed in the center of the screen this time, set the background color outside the image. Since the background of the image itself was set with ■ # 6A91ED , the same color is defined in color.xml.

color.xml


…
<color name ="background_color">#6A91ED</color>
…

Create splash.xml

Right-click on the drawable folder → select New → Drawable Resource File to create splash.xml. The name is splash and the root element is layer-lits. Describe as follows in splash.xml.

splash.xml


<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <!--Background color setting-->
    <item
        android:drawable="@color/background_color"/> 

  <!--Image settings to display-->
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash_logo"/>
    </item>

</layer-list>

This completes splash.xml.

(2) Describe the style to which splash.xml is applied.

This time, there is themes.xml in res/values, so I will describe it in it. It seems that styles.xml was created in res/values ​​in the past ... Well, as long as you can describe style, there is no problem. Add as follows to themes.xml. In addition, although the original Theme is set for parent, it may be refreshing to set NoActionBar in some cases.

Themes.xml


  …
    <style name="SplashTheme" parent="Theme.InfoDevice">
        <item name="android:windowBackground">@drawable/splash</item>
    </style>
  …

(3) Change Theme when MainActivity starts

Apply Theme for splash screen to AndroidManifest.xml. At this time, please remember the original Theme firmly. (In this example, the theme called Theme_InfoDevice was originally applied.)

Change before

AndroidManifest.xml


        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

After change

AndroidManifest.xml


        <activity
            android:name=".MainActivity"
            android:theme="@style/SplashTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

under android: name

android:theme="@style/SplashTheme"

I am adding. (If originally stated, rewrite @ style/... below to Theme for the splash screen.)

(4) Wait a few seconds for MainActivity and return to the original theme

After starting MainActivity, the theme of the splash screen has been applied, so wait a few seconds and then return to the original theme. The contents to be described in MainActivity are as follows.

MainActivity.java


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setTheme(R.style.Theme_InfoDevice);   //Originally applied theme
        setContentView(R.layout.activity_main);

        …

    }

This completes it.

Recommended Posts

[Java] 4 steps to implement splash screen on Android
Steps to register Java files on GitHub
[Android Studio] [Java] How to fix the screen vertically
[Java] How to implement multithreading
Steps to run docker on Mac
How to "hollow" View on Android
[Java] How to update Java on Windows
Steps to install MySQL 8 on CentOS 8
How to make a splash screen
[Java] Try to implement using generics
Try to implement Yubaba in Java
Notes on Android (java) thread processing
Steps to install devtoolset-6 on CentOS 7
[Android] Convert Android Java code to Kotlin
Implement ripple representation on images on Android
How to automatically operate a screen created in Java on Windows
How to call with One Touch (without confirmation) on Android (Java)
How to check Java installed on Mac
How to implement date calculation in Java
How to implement Kalman filter in Java
Sample to display (head-up) notifications on Android
Try to implement n-ary addition in Java
How to detect microphone conflicts on Android
Convert all Android apps (Java) to Kotlin
Calling java from C ++ on Android NDK
Sobel filter using OpenCV on Android (Java)
How to switch Java versions on Mac
How to output Java string to console screen
How to implement coding conventions in Java
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to create a splash screen
Screen transition by [Android Studio] [Java] button
Java to C and C to Java in Android Studio
Scheduled to support Reiwa on Android 11 as standard
Try to build Java8 environment on Amazon Linux2
[Android] Get the tapped position (coordinates) on the screen
[Android] List all setting items on the setting screen
I tried to implement deep learning in Java
Make software that mirrors Android screen to PC 1
[Android] Implement a function to display passwords quickly
Try to implement TCP / IP + NIO with JAVA
Try communication using gRPC on Android + Java server
[Java] Memo on how to write the source
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[Java] Introduction to Java
Introduction to java
I want to implement it additionally while using kotlin on a site running Java
[Android / Java] Screen transition and return processing in fragments
[Android Studio] How to change TextView to any font [Java]
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
What to do if TextToSpeech doesn't work on Android 11
How to use java non-standard library on IntelliJ IDEA
[Android / Java] Set up a button to return to Fragment
Steps to set up Postfix and Dovecot on CentOS 8.3
Steps to set up a VNC server on CentOS 8.3
How to use Truth (assertion library for Java / Android)
As of April 2018 How to get Java 8 on Mac
[Java] How to execute tasks on a regular basis
[Android Studio] I want to use Maven library on Android
Introducing New Relic to Java apps running on Heroku