1

For my app I am creating an Splash Screen using a drawable resource file consist of a layerlist having a colored background and a centered vector drawable for the logo.

splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/grey_700" />
    <item
        android:drawable="@drawable/splash_foreground"
        android:gravity="center" />
</layer-list>

style.xml

...

<style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:statusBarColor">@color/grey_900</item>
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

...

AndroidManifest.xml

...

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.SplashScreen"
            android:screenOrientation="portrait"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

...

splash_foreground.xml

enter image description here

Problem

the output should show splash_foreground centered in the screen while the background color is cropped all over the screen but is showing a splash screen centered cropped.

enter image description here

Sidharth Mudgil
  • 1,293
  • 8
  • 25

1 Answers1

1

I solved this problem by using the new splash_screen API. all thanks to this post

Solution

  • Add Splash Screen API in build.gradle
implementation 'androidx.core:core-splashscreen:1.0.0'
  • Add this line before the application tag in the AndroidManifest.xml file
<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />
  • In theme.xml & theme.xml (night) create a new style with Theme.SplashScreen being parent
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="android:statusBarColor">@color/grey_900</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item>
        <item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item>
        <item name="windowSplashScreenBackground">@color/grey_700</item>
        <item name="postSplashScreenTheme">@style/Theme.LGConnect</item>
    </style>
  • Set application theme and Launcher Activity theme to the newly created style
...
<application
...
   android:theme="@style/Theme.App.Starting"
...
      <activity
         ...
         android:theme="@style/Theme.App.Starting"
...
  • Inside onCreate method of MainActivity add this line
...
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      installSplashScreen()
...
Sidharth Mudgil
  • 1,293
  • 8
  • 25
  • 1
    Better Solution, just thought you used an extra Activty/Fragment to display your Splash Screen but when using the API this is significantly better... – TIMBLOCKER Apr 14 '23 at 10:48