Android Jetpack

Create a splash screen in Android without using an extra activity

Pinterest LinkedIn Tumblr

Yes, it is possible to create a splash screen in Android without using an extra activity. In this tutorial, we’ll show you how to create a splash screen in your Android app without using an extra activity, using a simple and efficient solution.

Understand the purpose of a splash screen

A splash screen is a graphical element that appears when an app is launched. Its purpose is to provide a visual cue to the user that the app is loading and to give the app time to initialize its resources.

splash screen android

Add dependency with Compose template

You can achieve this by using a theme with a windowBackground that displays an image or an animation for the desired amount of time before the main activity is shown.

So before you can create a splash screen in Android, you’ll need to set up your project and import the necessary dependencies. so let’s create a new project with compose template and the below dependency

    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'

Create a new theme in your styles.xml file

In this style, I am hiding the action bar and title for displaying the splash screen.

    <style name="Theme.SplashScreenExample.NoActionBar">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">false</item>
    </style>

App Theme

I am using one theme for this tutorial is it here

    <style name="Theme.SplashScreenExample" parent="android:Theme.Material.Light.DarkActionBar">
        <!-- Status bar color. -->
        <item name="android:statusBarColor">@color/purple_700</item>
        <!-- Primary brand color. -->
        <item name="android:colorPrimary">@color/purple_500</item>
    </style>

Create the splash in your res/value directory

Following the key properties I am using here

  • windowSplashScreenBackground – Set the background color
  • windowSplashScreenAnimatedIcon – Change the app logo, Icon should be adaptive as per Google guideline
  • windowSplashScreenAnimationDuration – Set the duration of the animation screen
  • postSplashScreenTheme – Set the theme that you want to change after splash screen.
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.App.MySplash" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/purple_500</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo</item>
        <item name="windowSplashScreenAnimationDuration">2000</item>
        <item name="postSplashScreenTheme">@style/Theme.SplashScreenExample</item>

    </style>
</resources>

Set the SplashTheme as the theme for your activity in the AndroidManifest.xml file:

<activity
    android:name=".MainActivity"
    android:theme="@style/Theme.App.MySplash">
    ...
</activity>

In your MainActivity, Call installSplashScreen()

Let’s call the installSplashScreen() functions in MainActviity before the View.

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    installSplashScreen()
    setContent {
      SplashScreenAndroidTheme {
        // A surface container using the 'background' color from the theme
        Surface(
          modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
        ) {
          SmileyView()
        }
      }
    }
  }
}

That’s it! Now your app will display the splash screen for the desired amount of time before showing the main activity without the need for an extra activity.

Conclusion

In this tutorial, we have learned how to create a splash screen in Android without using an extra activity. If you have any queries please let write them down in the comment box, I will happy to answer that, Thanks for reading.

Read our other tutorials on Compose UI

Write A Comment