Search code examples
androidkotlinandroid-jetpack-composesplash-screen

Progress bar in splash screen api


can i show progress bar in splash screen API?

My Android application implements the splash screen API with a single activity. However I need to use a progress bar in the api splash screen to notify the user that there is data from the server being loaded. Can I use a progress bar?


Solution

  • You can't add views like ProgressBar to Splash Screen API. It's designed to provide a consistent and minimal splash screen experience that quickly transitions to the main app content.

    Instead, you can set animated icon in Splash screen and prevent Activity from being displayed until the data is loaded.

    class MainActivity : AppCompatActivity() {
    
        private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
        private var keep = true
    
        override fun onCreate(savedInstanceState: Bundle?) {
            val splashScreen = installSplashScreen()
            super.onCreate(savedInstanceState)
    
            splashScreen.setKeepOnScreenCondition { keep }
            setContentView(binding.root)
            startLongRunningTask()
        }
    
        private fun startLongRunningTask() {
            Timer().schedule(5000) { keep = false } // wait 5 sec
        }
    }
    

    If you still want to use custom views like ProgressBar, you can transition from the system splash screen into your custom splash screen Activity. But Google recommends avoiding this scenario if possible and use the SplashScreen API to brand your splash screen. For more information, please refer to this link.