How to correctly realize the method of Android startup screen (avoid white screen)

  • 2021-08-21 21:16:22
  • OfStack

An incorrect implementation of the Android boot screen may cause users to wait for a long time, or a black-and-white screen may appear. Here is a brief demonstration of how to correctly implement the Android startup screen.

The demonstration is divided into the following steps:

Create the splash_background. xml file in the res/drawable folder. res/values/styles. xml Create java/.../SplashActivity manifests/AndroidManifest. xml

1. Create the splash_background. xml file in the res/drawable folder

Adjust the gravity and size of bitmap images according to your needs.


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

  <item android:drawable="@color/colorPrimary"/>

  <item android:gravity="center" android:width="100dp" android:height="100dp">
    <bitmap
      android:gravity="fill_horizontal|fill_vertical"
      android:src="@drawable/logo"/>
  </item>

</layer-list>

2. Edit res/values/styles. xml

The style here is used for the startup screen. This is to hide the action bar when starting the screen.


<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
  </style>

</resources>

3. Create java/.../SplashActivity

1 Once App is started, SplashActivity will be started and then transferred to MainActivity.


package com.example.jtdan.goodSplash;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //switch from splash activity to main activity
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
  }
}

4. Edit manifests/AndroidManifest. xml

Add a new startup screen Activity to the manifest file.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.jtdan.goodSplash">

  <application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="goodSplash"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name="com.example.jtdan.goodSplash.SplashActivity" android:theme="@style/SplashTheme">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <activity android:name="com.example.jtdan.goodSplash.MainActivity"></activity>

  </application>

</manifest>

Sample source address: https://github.com/mrjoedang/goodSplash


Related articles: