Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 26, 2022 10:14 pm GMT

Android Splash Screen

Hello folks!
This is my first post here so I'll start with a beginner article about Android Development.

Thinking about any Android application that we use nowadays, the first thing that we stare is the Splash Screen, and since the beginning of my journey into Android development I always had difficult to find the correct way to create a Splash Screen, and this "problem" ended in the end of last year when it came out an official way to create a Splash Screen from Android team.

Note: In this article I'll show only in Kotlin language, because Android development is "Androids Kotlin-first approach' since 2019, but it's possible to use Java as well.

Let's start our project!

Project

To create the project you'll just need to follow the steps below:

  • Create New Project > Empty Compose ActivityImage description
  • Project configuration (name, package, etc.)Image description

After this part you'll be ready to implement the Splash Screen into your project.

Settings

Now we need to set up some configurations in our project.

Build.gradle

Your compileSdk needs to be at least 31 and you need to add the Splash Screen dependency.

android {   compileSdkVersion 32   //Other configuration}dependencies {   //Other dependencies   implementation 'androidx.core:core-splashscreen:1.0.0-rc01'}

Image description

Theme

It's need to create a new theme and for that you can follow the example below:
The parent of your theme needs to be Theme.SplashScreen that is the theme from the Android system, and also you need to set the theme of your following activity on the postSplashScreenTheme attribute.

<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">        <!-- Set the splash screen background, animated icon, and animation duration. -->        <item name="windowSplashScreenBackground">@color/white</item>        <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an             animated drawable. One of these is required. -->        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_duck</item>        <!-- Required for animated icons -->        <!-- <item name="windowSplashScreenAnimationDuration">5000</item> -->        <!-- Set the theme of the Activity that directly follows your splash screen. -->        <!-- Required -->        <item name="postSplashScreenTheme">@style/Theme.SplashScreenApp</item></style>

Note 1: If you want to add a background color for your icon, you can use the following theme Theme.SplashScreen.IconBackground and use the attribute windowSplashScreenIconBackground.

Note 2: The attribute windowSplashScreenAnimationDuration is only used with animations, for images you can remove it.

Image description

Manifest

Now you need to add this new theme to your application or activity. I prefer to add to the activity, but it's a personal choice.

<application        android:allowBackup="true"        android:dataExtractionRules="@xml/data_extraction_rules"        android:fullBackupContent="@xml/backup_rules"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/Theme.SplashScreen"        tools:targetApi="31">        <activity            android:name=".MainActivity"            android:exported="true"            android:label="@string/app_name"            android:theme="@style/Theme.App.SplashScreen">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity></application>

Image description

Activity

In your Activity you need to call the method installSplashScreen() after super.onCreate(). The method installSplashScreen returns the Splash Screen object so you can customize the behavior with these two methods: setKeepOnScreenCondition and setOnExitAnimationListener. You can use these methods together or independently, it depends on the need of your project.

setKeepOnScreenCondition

Like the name already says it'll keep the Splash Screen until something happens, in my case I have a viewModel handling how long it will take to disappear.

class MainActivity : ComponentActivity() {    private val mainViewModel: MainViewModel by viewModels()    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        installSplashScreen().apply {            setKeepOnScreenCondition {                mainViewModel.isLoading.value            }        }        setContent {            SplashScreenTheme {                Box(                    modifier = Modifier.fillMaxSize(),                    contentAlignment = Alignment.Center                ) {                    Text(text = "Hello World!")                }            }        }    }}

setOnExitAnimationListener

In this case, as soon as your Splash Screen finishes you can execute anything. E.g.: Load data, validate cache, etc.

class MainActivity : ComponentActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        installSplashScreen().apply {            setOnExitAnimationListener{                //Do something after the Splash Screen                it.remove()            }        }        setContent {            SplashScreenTheme {                Box(                    modifier = Modifier.fillMaxSize(),                    contentAlignment = Alignment.Center                ) {                    Text(text = "Hello World!")                }            }        }    }}

And that's all you need to create a Splash Screen following the official approach from Android Developers' site.

Image description

Git repository

Github

References

Android Developers
Youtube


Original Link: https://dev.to/diegodeabreu/android-splash-screen-o0a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To