<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Diego de Abreu</title>
    <description>The latest articles on DEV Community by Diego de Abreu (@diegodeabreu).</description>
    <link>https://dev.to/diegodeabreu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F866577%2F3a0349aa-dbef-4bd4-a4a5-ec37a50f90fb.jpeg</url>
      <title>DEV Community: Diego de Abreu</title>
      <link>https://dev.to/diegodeabreu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/diegodeabreu"/>
    <language>en</language>
    <item>
      <title>Android Splash Screen</title>
      <dc:creator>Diego de Abreu</dc:creator>
      <pubDate>Thu, 26 May 2022 22:14:00 +0000</pubDate>
      <link>https://dev.to/diegodeabreu/android-splash-screen-o0a</link>
      <guid>https://dev.to/diegodeabreu/android-splash-screen-o0a</guid>
      <description>&lt;p&gt;Hello folks!&lt;br&gt;
This is my first post, so I'll start with a beginner article about Android Development.&lt;/p&gt;

&lt;p&gt;Thinking about any Android application we use nowadays, the first thing we stare at is the Splash Screen. Since the beginning of my journey into Android development, I have always had difficulty finding the correct way to develop it. The "problem" ended at the end of last year when it came out in an official way provided by the Android Developer's team.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: In this article, I'll show only the Kotlin language because Android development has been "Android's Kotlin-first" approach since 2019, but it's possible to use Java as well.&lt;/p&gt;

&lt;p&gt;Let's start our project!&lt;/p&gt;
&lt;h1&gt;
  
  
  Project
&lt;/h1&gt;

&lt;p&gt;To create the project you'll just need to follow the steps below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create New Project &amp;gt; Empty Compose Activity
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frto6d7l0uzfmz7lwkwvx.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;li&gt;Project configuration (name, package, etc.)
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l3gf5xnpx9q8cxphfk0.png" alt="Image description"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After this part you'll be ready to implement the Splash Screen into your project.&lt;/p&gt;
&lt;h2&gt;
  
  
  Settings
&lt;/h2&gt;

&lt;p&gt;Now we need to set up some configurations in our project.&lt;/p&gt;
&lt;h3&gt;
  
  
  Build.gradle
&lt;/h3&gt;

&lt;p&gt;Your compileSdk needs to be at least 31 and you need to add the Splash Screen dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;android {
   compileSdkVersion 32
   //Other configuration
}
dependencies {
   //Other dependencies
   implementation 'androidx.core:core-splashscreen:1.0.0-rc01'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwad5vn09m0u5mah5cq21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwad5vn09m0u5mah5cq21.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Theme
&lt;/h3&gt;

&lt;p&gt;It's needed to create a new theme, and for that, you can follow the example below:&lt;br&gt;
The parent of your theme needs to be &lt;strong&gt;Theme.SplashScreen&lt;/strong&gt;, which is the theme from the Android system. Also, you need to set the theme of your following activity on the &lt;strong&gt;postSplashScreenTheme&lt;/strong&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style name="Theme.App.SplashScreen" parent="Theme.SplashScreen"&amp;gt;
        &amp;lt;!-- Set the splash screen background, animated icon, and animation duration. --&amp;gt;
        &amp;lt;item name="windowSplashScreenBackground"&amp;gt;@color/white&amp;lt;/item&amp;gt;

        &amp;lt;!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
             animated drawable. One of these is required. --&amp;gt;
        &amp;lt;item name="windowSplashScreenAnimatedIcon"&amp;gt;@drawable/ic_duck&amp;lt;/item&amp;gt;
        &amp;lt;!-- Required for animated icons --&amp;gt;
        &amp;lt;!-- &amp;lt;item name="windowSplashScreenAnimationDuration"&amp;gt;5000&amp;lt;/item&amp;gt; --&amp;gt;

        &amp;lt;!-- Set the theme of the Activity that directly follows your splash screen. --&amp;gt;
        &amp;lt;!-- Required --&amp;gt;
        &amp;lt;item name="postSplashScreenTheme"&amp;gt;@style/Theme.SplashScreenApp&amp;lt;/item&amp;gt;
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note 1&lt;/strong&gt;: If you want to add a background color for your icon, you can use the following theme &lt;strong&gt;Theme.SplashScreen.IconBackground&lt;/strong&gt; and use the attribute &lt;strong&gt;windowSplashScreenIconBackground&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note 2&lt;/strong&gt;: The attribute &lt;strong&gt;windowSplashScreenAnimationDuration&lt;/strong&gt; is only used with animations. For images, it's not necessary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7i44vozu7v0vg0qb792d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7i44vozu7v0vg0qb792d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Manifest
&lt;/h3&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;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"&amp;gt;
        &amp;lt;activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.App.SplashScreen"&amp;gt;
            &amp;lt;intent-filter&amp;gt;
                &amp;lt;action android:name="android.intent.action.MAIN" /&amp;gt;

                &amp;lt;category android:name="android.intent.category.LAUNCHER" /&amp;gt;
            &amp;lt;/intent-filter&amp;gt;
        &amp;lt;/activity&amp;gt;
&amp;lt;/application&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw8mxad4y2xdrueki9se7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw8mxad4y2xdrueki9se7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Activity
&lt;/h3&gt;

&lt;p&gt;In your Activity you need to call the method &lt;strong&gt;&lt;em&gt;installSplashScreen()&lt;/em&gt;&lt;/strong&gt; after &lt;strong&gt;super.onCreate()&lt;/strong&gt;. The method installSplashScreen returns the Splash Screen object, so you can customize the behavior with these two methods: &lt;strong&gt;setKeepOnScreenCondition&lt;/strong&gt; and &lt;strong&gt;setOnExitAnimationListener&lt;/strong&gt;. You can use these methods together or independently, depending on your project's need.&lt;/p&gt;

&lt;h4&gt;
  
  
  setKeepOnScreenCondition
&lt;/h4&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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!")
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  setOnExitAnimationListener
&lt;/h4&gt;

&lt;p&gt;In this case, as soon as your Splash Screen finishes, you can execute anything. E.g., Load data, validate cache, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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!")
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all you need to create a Splash Screen following the official Android Developers' site approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25gnr7c2qblcgmu7mey9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F25gnr7c2qblcgmu7mey9.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Git repository
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/diegodeabreu/splash-screen" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://developer.android.com/guide/topics/ui/splash-screen/migrate" rel="noopener noreferrer"&gt;Android Developers&lt;/a&gt;&lt;br&gt;
&lt;a href="https://youtu.be/Loo4i5IrZ4Y" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>beginners</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
