Android 12 Splash Screen API not working with Material Component
Asked Answered
I

2

7

I've been following the documentation, but unfortunately it doesn't include the adaptation when using Material Component as app overall theme.

<style name="Theme.App" parent="Theme.MaterialComponents.DayNight">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
    <item name="colorOnPrimary">@android:color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/colorPrimary</item> <!--Fab color-->
    <item name="colorOnSecondary">@color/colorAccent</item> <!--Fab icon color-->
    <item name="colorSecondaryVariant">@color/colorAccentDark</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <!--<item name="tabStyle">@style/AppTabLayout</item>-->
    <!-- The color for all other text including the menu -->
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorHighlight">@color/colorAccentDark</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>

    <item name="checkboxStyle">@style/AppCheckBoxStyle</item>
    <item name="popupMenuBackground">@drawable/popup_bg_rounded</item>
    <item name="materialAlertDialogTheme">@style/AppDialogTheme</item>

    <item name="autoCompleteTextViewStyle">@style/AppCursor</item>

    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>

    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
</style>

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">
            @color/whitePrimaryDark
        </item>
        <item name="postSplashScreenTheme">
            @style/Theme.Cryptonian.NoActionBar
        </item>
    </style>

Android Manifest

    <activity
        android:name=".presentation.MainActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.App.Starting"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

It crash with an error

  Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).

Does anyone face same issue?

Ide answered 2/4, 2022 at 16:58 Comment(1)
Are calling installSplashScreen() before setting layout content of your activity? Ref: developer.android.com/reference/androidx/core/splashscreen/…Cichocki
I
7

The solution is to install the Splash Screen API before calling super.onCreate() and setContentView

Ide answered 3/8, 2022 at 19:27 Comment(1)
In my case, calling it before super.onCreate() does not show splash screen. But calling it right after super.onCreate() and before anything else works fineMarkle
M
1

You need to change this:

<item name="postSplashScreenTheme">@style/Theme.Cryptonian.NoActionBar</item>

to this:

<item name="postSplashScreenTheme">@style/Theme.App</item>

postSplashScreenTheme requires to get you app theme. after showing splash screen your app theme wi

Mystique answered 3/8, 2022 at 16:2 Comment(2)
Sorry I already solved this and this is not the solution. Thanks anywayIde
Thanks, I don't know how I missed that even though it's in the documentation. It might not was the solution for OP, but it was indeed a solution for this error.Edger

© 2022 - 2024 — McMap. All rights reserved.