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'
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>
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>
Lastly, don't forget to add this code before setContentView(R.layout.activity_main);
:
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
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.
Top comments (0)