. f Splash Screen API in Android 12 | Android 12 Splash Screen Example | Splash screens in Android 12 | Splash screen demo Using Splash Screen API | Splash Screen API Example ~ Android Developers Blog

Thursday, 21 October 2021

Splash Screen API in Android 12 | Android 12 Splash Screen Example | Splash screens in Android 12 | Splash screen demo Using Splash Screen API | Splash Screen API Example

 Hi All,
         Today I am going to share a tutotial and a sample demo of Android 12 
Splash Screen Api.  Android 12 Splash screen api introduce in Android 12.

Android 12 adds the SplashScreen API, which enables a new app launch animation
for all apps when running on a device with Android 12 or higher. This includes an
into-app motion at launch, a splash screen showing your app icon, and a transition
to your app itself. 




Note: As Splash Screen  introduced in Android 12 so it will work on Android 
Api level 31 and above. 

We can Customize the Splash Screen Theme:
                - With the help of below attribute we can customize our Splash activity theme.

1. android:windowSplashScreenBackground : Used for Splash Screen background color.

  1. <!--splash screen background color-->  
  2. <item name="android:windowSplashScreenBackground">#FFFFFF</item>      

2. android:windowSplashScreenAnimatedIcon:  Used to replace an icon in
the center of the starting window. If the object is animatable and drawable through AnimationDrawable and AnimatedVectorDrawable, you also need to set windowSplashScreenAnimationDuration to play the animation while showing the
starting window.
  1. <!--provide animatedly drawable-->  
  2. <item name="android:windowSplashScreenAnimatedIcon">  
  3.     @drawable/splash_screen_animateable_icon  
  4. </item>   
3. android:windowSplashScreenAnimationDuration : Used to indicate the duration of the splash screen icon animation. Max duration is 1000ms.


  1. <!--max duration is 1000ms-->  
  2. <item name="android:windowSplashScreenAnimationDuration">800</item>   

4. 
android:windowSplashScreenIconBackgroundColor : Used to set a background behind
 the splash screen icon. This is useful if there isn’t enough contrast between the window
 background and the icon.

  1. <!--icon background color-->  
  2. <item name="android:windowSplashScreenIconBackgroundColor">#004d28</item>     

5. android:windowSplashScreenBrandingImage: We can use this to set an image to be shown at the bottom of the splash screen. The design guidelines recommend against using a branding image.

  1. <!--splash screen branding image-->  
  2. <item name="android:windowSplashScreenBrandingImage">@drawable/ic_logo_12</item>      

Keep the splash screen on-screen for longer periods:

The splash screen is dismissed as soon as your app draws its first frame. If you need to load a small amount of data such as in-app settings from a local disk asynchronously, you can use ViewTreeObserver.OnPreDrawListener to suspend the app to draw its first frame.

If your starting activity finishes before drawing (for example, by not setting the content view and finishing before onResume), the pre-draw listener is not needed.



  1. override fun onCreate(savedInstanceState: Bundle?) {  
  2.         super.onCreate(savedInstanceState)  
  3.         setContentView(R.layout.activity_main)  
  4.         mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)  
  5.         content = findViewById(android.R.id.content)  
  6.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {  
  7.             Log.d("MainActivity""onCreate: I AM RUNNING ON API 12 or higher")  
  8.             content.viewTreeObserver.addOnPreDrawListener(object :  
  9.                     ViewTreeObserver.OnPreDrawListener {  
  10.                     override fun onPreDraw(): Boolean =  
  11.                         when {  
  12.                             mainViewModel.mockDataLoading() -> {  
  13.                                 content.viewTreeObserver.removeOnPreDrawListener(this)  
  14.                                 true  
  15.                             }  
  16.                             else -> false  
  17.                         }  
  18.                 })  
  19.         }  
  20.     }     
Customizing Splash Screen Animation: In Android 12 we get the Splash screen 
instance by calling getSplashScreen() from Activity class. So we can further customize
the animation of the splash screen through Activity.getSplashScreen().
  1. splashScreen.setOnExitAnimationListener { splashScreenView ->  
  2.     val slideUp = ObjectAnimator.ofFloat(  
  3.         splashScreenView,  
  4.         View.TRANSLATION_Y,  
  5.         0f,  
  6.         -splashScreenView.height.toFloat()  
  7.     )  
  8.     slideUp.interpolator = AnticipateInterpolator()  
  9.     slideUp.duration = 200L  
  10.   
  11.     // Call SplashScreenView.remove at the end of your custom animation.  
  12.     slideUp.doOnEnd { splashScreenView.remove() }  
  13.   
  14.     // Run your animation.  
  15.     slideUp.start()  
  16. }     
Code: MainActivity.kt
  1. package com.example.android12splashscreenapi  
  2.   
  3. import android.animation.ObjectAnimator  
  4. import android.os.Build  
  5. import android.os.Bundle  
  6. import android.util.Log  
  7. import android.view.View  
  8. import android.view.ViewTreeObserver  
  9. import android.view.animation.AnticipateInterpolator  
  10. import androidx.appcompat.app.AppCompatActivity  
  11. import androidx.core.animation.doOnEnd  
  12. import androidx.lifecycle.ViewModelProvider  
  13.   
  14. class MainActivity : AppCompatActivity() {  
  15.     lateinit var content: View  
  16.     lateinit var mainViewModel: MainViewModel  
  17.     override fun onCreate(savedInstanceState: Bundle?) {  
  18.         super.onCreate(savedInstanceState)  
  19.         setContentView(R.layout.activity_main)  
  20.         mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)  
  21.         content = findViewById(android.R.id.content)  
  22.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {  
  23.             Log.d("MainActivity""onCreate: I AM RUNNING ON API 12 or higher")  
  24.             content.viewTreeObserver.addOnPreDrawListener(object :  
  25.                     ViewTreeObserver.OnPreDrawListener {  
  26.                     override fun onPreDraw(): Boolean =  
  27.                         when {  
  28.                             mainViewModel.mockDataLoading() -> {  
  29.                                 content.viewTreeObserver.removeOnPreDrawListener(this)  
  30.                                 true  
  31.                             }  
  32.                             else -> false  
  33.                         }  
  34.                 })  
  35.   
  36.             // custom exit on splashScreen  
  37.             splashScreen.setOnExitAnimationListener { splashScreenView ->  
  38.                 val slideUp = ObjectAnimator.ofFloat(  
  39.                     splashScreenView,  
  40.                     View.TRANSLATION_Y,  
  41.                     0f,  
  42.                     -splashScreenView.height.toFloat()  
  43.                 )  
  44.                 slideUp.interpolator = AnticipateInterpolator()  
  45.                 slideUp.duration = 200L  
  46.   
  47.                 // Call SplashScreenView.remove at the end of your custom animation.  
  48.                 slideUp.doOnEnd { splashScreenView.remove() }  
  49.   
  50.                 // Run your animation.  
  51.                 slideUp.start()  
  52.             }  
  53.         }  
  54.     }  
  55. }  
  56.       



Reference
:

Note: For updating/migraating your older app  for Android 12 please check below
tutorial.


Download Complete Code Here
Hope this will help Someone.
Enjoy Coding ..........................  :)



Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.