How to implement a splash screen in Android? — Get rid of the white background…
Hello, I am get rid of the white background of the splash screen from the app starting and to load the content (layout file) of the activity.
I searched a bit and found something.
In android applications there are themes to style your applications. You can configure your status bar color, app bar color etc.
Also you can use different themes for your every activity.
We will use a different theme for the splash screen.
So, how to implement the splash?
Firstly, we shouldn’t use a layout for a splash activity.
Just create a new drawable for your splash screen (I simply used the icon), a new theme for your splash activity in your styles.xml and use in AndroidManifest when declare your Splash Activity.
The code should be like this:
styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/bg_splash</item>
</style>
</resources>
Setting the windowBackground property is the magic :)
in AndroidManifest:
<activity android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And create a bg_splash.xml drawable like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimary" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_splash" />
</item>
</layer-list>
PS: @drawable/ic_splash is just a png drawable.
SplashActivity doesn’t contain any layout and should be like this:
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Handler(Looper.getMainLooper()).postDelayed(Runnable {
startActivity(Intent(this, MainActivity::class.java))
finish()
}, 2000)
}
}
When you done the steps succesfully, you should directly see your splash screen when you start your application.
Thanks for reading :)