Many apps now add a launch page before entering the home screen, and then add a few seconds of ads. I don’t think this is a “real” launch page, it should be called an AD page.

When an Android app starts cold, it starts from the Application, which takes a long time to load. During this time, the user will only see a “white screen” (this is because the default AppTheme for Android :windowBackground is set to white by default). So I think the actual launch page is not a “white screen” when the user opens the app, but a page that we create. It could be an image, it could be a piece of text. In this way, the user who does not know the truth intuitively feels that the app can be opened in seconds.

1. Create a splash_screen. XML file in the Drawable directory

<? The XML version = "1.0" encoding = "utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <item android:drawable="@color/colorPrimary"/> <item> <bitmap android:src="@drawable/ic_logo" android:gravity="center"/> </item> </layer-list>Copy the code

We use the Layer-List TAB to create a list of layers, which is actually a LayerDrawable, set a background, and then put the app icon. This is the launch page I want to show you, and you can define it as you want.

2. Then define a SplashTheme in the style. XML file

<resources>
    ...
    
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/splash_screen</item>
    </style>

</resources>
Copy the code

We just need to set the window background to the LayerDrawable that we just defined.

3. Then we need to set the Android: Theme of our main page (in this case, MainActivity) to the SplashTheme we defined in the AndroidMenifest.xml file

<? The XML version = "1.0" encoding = "utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" . >... <application ... > <activity android:name=".activity.MainActivity" android:launchMode="singleTask" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> ... </application> </manifest>Copy the code

Is it easy to just do that