Android : Splash Screen using styles / themes.xml

Anubhav
Geek Culture
Published in
2 min readJun 15, 2021
Photo by Marcus Ganahl on Unsplash

We talked about creating a Splash Screen the old school way in one of my previous articles. In this article, I will talk about a way to implement splash screen by just using styles/themes.xml. This will eliminate the need to create another activity just to show the splash screen.

So to begin we need to create a style for our Splash Screen in styles/themes.xml file.

<style name="SplashScreenTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/ic_splash_screen</item>
</style>

I have created a style named SplashScreenTheme, with the parent with no action bar. I have added an item with the property windowBackground, this is where we need to put in our splash screen style / drawable. I am using an image from my drawable folder.

To show the splash screen before our launcher activity, we need to replace our applications theme with the theme we just created.
So go to the manifest file and and set the application theme as SplashScreenTheme,

<application
...
android:theme="@style/SplashScreenTheme">

....

</application>

Now our applications style is set to SplashScreenTheme.

Next to give our application our old theme after the splash screen is done showing, we need to set the app theme which we were previously using in our launcher activities onCreate(..) method.

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.YOUR_THEME)

....
}

Before our launcher activity’s onCreate(..) is called we show the splash screen theme, and after it’s onCreate(..) is called we switch to our app theme.

If we run our application now, the splash screen will show up.
If you notice we can see the status bar and the navigation buttons on the splash screen, which we can hide to give a better experience to the user. For the same we need to set two more properties in the theme we created,

<style name="SplashScreenTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/ic_splash_screen</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

</style>

The above properties are self explanatory, they make the status and navigation bar translucent.

That is it folks, you have your splash screen up and running without needing to create another activity.

--

--

Responses (1)