DEV Community

Wends
Wends

Posted on

1 1

New Android Splash Screen

I'm a fan of splash screen, for every project I try to create I always wanted to apply splash screen because it's fun to look at! But I never really liked the previous version of applying it so I don't apply it much. But yesterday, since I started working on my portfolio again, I encountered a new good way to implement it.

Put this package in build.gradle (module) in dependencies

implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
Enter fullscreen mode Exit fullscreen mode

If you can't see 'styles.xml' in your project, go ahead and right click on your res folder and create a new xml file, name it as 'styles.xml' and put this code.

<resources>
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_stat_shop</item>
        <item name="windowSplashScreenBackground">@color/teal_700</item>
        <item name="postSplashScreenTheme">@style/Theme.OnlineShop</item>

    </style>
</resources>
Enter fullscreen mode Exit fullscreen mode

The windowSplashScreenAnimatedIcon is your animated or simple logo, the one you want to see in the middle of your splash screen.

The windowSplashScreenBackground item is the background color of your splash screen, mine is just from the color xml file but you can also put there the background color of your ic_launcher/logo if you have one.

The AppTheme in your postSplashScreenTheme item is the theme name your project is currently using, you can find it inside themes folder inside the themes.xml files.

Add the style to your mainactivity in androidmanifest:

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Enter fullscreen mode Exit fullscreen mode

Lastly, don't forget to add this code before setContentView(R.layout.activity_main);:

SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
Enter fullscreen mode Exit fullscreen mode

or else you'll encounter this error, as I did:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.onlineshop/com.test.onlineshop.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay