- Qt 6.10
- Implementing a splash screen with Qt on Android
- Qt 6.10.3 Reference Documentation

# Implementing a splash screen with Qt on Android

Showcase how to setup a splash screen with Qt for Android.

This is a simple example which demonstrates one way to show a splash screen with Qt. When the app starts, The Qt splash screen with a logo and a background color is shown for two seconds. The Qt splash screen is then faded out to show the app main view. The main view consists of a simple label and a close button.

In this example, the Android default splash screen (Since Android 12) is hidden.

This example is one of series of two similar examples demonstrating splash screen. This one uses AndroidManifest flags the other uses QML view.

#### Using the Qt splash screen on Android

The Qt splash screen for Android is defined in the AndroidManifest.xml. The Android project files can be created manually or using the Qt Creator template generator. To create an Android project file using the Qt Creator template generator:

1. Open Projects
2. Go to Build Steps subsection
3. Open Build Android APK
4. Select Create Templates from Application subsection

                 <meta-data android:name="android.app.splash_screen_drawable"
                     android:resource="@drawable/qtsplashscreen"/>
                 <meta-data android:name="android.app.splash_screen_sticky"
                     android:value="true"/>

In the previous snippet:

- android.app.splash_screen_drawable defines the resource file defining the Qt splash screen.
- android.app.splash_screen_sticky keeps the Qt splash screen on the screen until QNativeInterface::QAndroidApplication::hideSplashScreen() function is called.

           QNativeInterface::QAndroidApplication::hideSplashScreen(2000);

Since Android 12, Android has a default splash screen. On launch, the default splash screen is shown before the first view of the application is shown. There are instructions and an API to modify it. See Android Splash screens.
By default, Android uses applications icon for the splash screen. On Qt applications this can be used as is, or modified to be used as the applications splash screen or it can be combined with Qt splash screen. To keep the launch user experience consistent between platforms, it may be needed to hide the Android splash screen. To do this, define a translucent theme that effectively hides the Android splash screen.

     <?xml version="1.0" encoding="UTF-8"?>
     <resources>
         <style name="splashStartTheme">
             <item name="android:windowNoTitle">true</item>
             <item name="android:windowDisablePreview">true</item>
             <item name="android:windowIsTranslucent">true</item>
         </style>
     </resources>

There is a problem though. Orientation changes stop working if we have translucent theme. This is caused by the fact that the Android view gets its orientation from the underlying view, usually this is an Application Drawer which does not change its orientation. And because the theme cannot be changed after the Activity view is set, we end up with an application stuck in one orientation.
As a workaround we can create throwaway Activity with translucent theme for the Android splash screen. As soon as possible we launch another Activity, which has all the bells and whistles our application requires.

             <activity
                 android:name=".SplashActivity"
                 android:exported="true"
                 android:theme="@style/splashStartTheme"
                 android:noHistory="true">
                 <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                 </intent-filter>
             </activity>

To launch the Qt splash screen, we also need to override the Android Activity `onCreate` and use an Intent to launch the QtActivity.

     package io.qt.qtsplashscreeninandroid;

     import android.app.Activity;
     import android.os.Bundle;
     import android.view.View;
     import android.util.Log;
     import android.content.Intent;
     import android.view.ViewTreeObserver;
     import org.qtproject.qt.android.bindings.QtActivity;

     public class SplashActivity extends Activity {

         private static final String TAG = "SplashActivity";

         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             // Set up an OnPreDrawListener to the root view.
             final View content = findViewById(android.R.id.content);
                     content.getViewTreeObserver().addOnPreDrawListener(
                             new ViewTreeObserver.OnPreDrawListener() {
                 @Override
                 public boolean onPreDraw() {
                     Intent intent = new Intent(SplashActivity.this, QtActivity.class);
                     intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                     startActivity(intent);

                     content.getViewTreeObserver().removeOnPreDrawListener(this);
                     finish();
                     return true;
                 }
             });
         }
     }

In this example the main view contains just a single view with an exit button.

     import QtQuick
     import QtQuick.Controls

     Window {
         visible: true
         title: qsTr("Qt Splash Screen Example")
         color: "#2CDE85"

         Text {
             id: textLabel
             text: "First View"
             anchors.centerIn: parent
         }
         Button {
             id: closeButton
             anchors.horizontalCenter: parent.horizontalCenter
             anchors.top: textLabel.bottom
             anchors.topMargin: 5
             background: Rectangle {
                 color: "#00414A"
                 radius: 2
             }
             text: "Close"
             contentItem: Text {
                 color: "#FFFFFF"
                 text: closeButton.text
                 horizontalAlignment: Text.AlignHCenter
                 verticalAlignment: Text.AlignVCenter
             }
             onClicked: {
                 Qt.callLater(Qt.quit)
             }
         }
     }

Example project @ code.qt.io