Skip to main content

Lottie animation as a splash activity in Android 12+

Setting a Lottie animation as a splash activity in Android 12+ involves a few key steps, primarily leveraging the SplashScreen API introduced in Android 12 (API level 31). Here's a comprehensive guide:

1. Add Dependencies:

  • Make sure your project uses Android Gradle Plugin 7.0.0 or higher and Gradle 7.0 or higher.
  • Add the necessary dependencies to your module-level build.gradle file:

dependencies {
implementation 'androidx.core:core-splashscreen:1.0.1' // Or latest version
implementation 'com.airbnb.android:lottie:latest_version' // Or latest version
}

2. Create Your Lottie Animation:

  • Design or obtain your Lottie animation file (.json).
  • Place the animation file in your res/raw folder. If the raw folder doesn't exist, create it.

3. Implement the Splash Screen:

  • In your AndroidManifest.xml, set the theme of your splash activity to a theme that extends Theme.SplashScreen:

<resources>
<style name="Theme.AppSplash" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/your_splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/lottie_splash_drawable</item>
<item name="postSplashScreenTheme">@style/Theme.YourApp</item>
</style>

<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
</style>
</resources>

<application
android:theme="@style/Theme.YourApp">

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.AppSplash">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
  • windowSplashScreenBackground: Sets the background color of the splash screen.
  • windowSplashScreenAnimatedIcon: This is where you'll use a Drawable that references your Lottie animation.
  • postSplashScreenTheme: The theme to apply after the splash screen is dismissed.

4. Create a Lottie Drawable:

  • Create an XML file in your res/drawable folder (e.g., lottie_splash_drawable.xml) to define the Lottie drawable:
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">

<aapt:attr name="android:drawable">
<animated-vector>
<aapt:attr name="android:drawable">
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="200"
android:viewportHeight="200">

<group>
<aapt:attr name="android:animation">
<objectAnimator
android:duration="2000"
android:propertyName="lottie_rawRes"
android:valueType="rawRes"
android:value="@raw/your_lottie_animation" />

</aapt:attr>
</group>
</vector>
</aapt:attr>
</animated-vector>
</aapt:attr>
</animated-vector>
  • Replace your_lottie_animation with the name of your Lottie file (without the .json extension).
  • Adjust the widthheight, and viewportWidthviewportHeight to match your animation's dimensions.
  • Change the duration of the animation in the objectAnimator element.

5. Implement the Splash Screen in your Activity:

  • In your MainActivity's onCreate method, call installSplashScreen() before super.onCreate():
import androidx.core.splashscreen.SplashScreen;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen splashScreen = installSplashScreen();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Optionally, you can keep the splash screen visible for a specific duration:
splashScreen.setKeepOnScreenCondition(() -> {
// Return true to keep the splash screen visible.
// Return false to dismiss it.
return false; // For example, dismiss immediately.
});

// Your other activity code...
}
}
  • installSplashScreen(): This method sets up the splash screen.
  • setKeepOnScreenCondition(): This method allows you to control how long the splash screen is displayed. You can use it to perform background tasks or wait for data to load before dismissing the splash screen. If you return false, the splash screen will dismiss immediately after the animated icon has finished playing.

Important Considerations:

  • Performance: Optimize your Lottie animation for performance to avoid delays or frame drops.
  • Animation Duration: Ensure the animation duration is appropriate for a splash screen. A short, smooth animation is generally preferred.
  • API Level: The SplashScreen API is available on Android 12 (API level 31) and higher. For older versions, you'll need to use a different approach.
  • Theme Consistency: Ensure that the splash screen’s background color and the animation’s style match your app’s overall theme.

By following these steps, you can effectively implement a Lottie animation as your splash activity in Android 12+.


Comments

Popular posts from this blog

How edit host file in windows 10 and windows 11

  Editing the hosts file in Windows 10 and Windows 11 can be done using a text editor like Notepad. However, since the hosts file is a system file, you will need to run Notepad as an administrator to make changes. Here are the steps on how to do it: Windows 10: Open Notepad. Right-click on the Notepad icon in the taskbar and select "Run as administrator. " In the Notepad window, click on the "File" menu and select "Open. " Navigate to the following location: C: \Windows\System32\drivers\etc Select the "hosts" file and click on the "Open" button. Make the desired changes to the hosts file. Click on the "File" menu and select "Save. " Windows 11: Open Notepad. Click on the file menu and select "Open with". Select "Notepad (Administrator)" from the list of apps. In the Notepad window, click on the "File" menu and select "Open. " Navigate to the following lo...

Top 10 Mobiles with Dolby Atmos in 2023

Dolby Atmos is a surround sound technology that creates a more immersive and realistic audio experience. It does this by using multiple speakers to create a three-dimensional soundstage. This means that you can hear sounds coming from all around you, including above and below you. Benefits of Dolby Atmos in Mobile: More immersive audio experience: Dolby Atmos can create a more immersive audio experience for movies, music, and games. This can make you feel like you are right in the middle of the action. Clearer and more detailed sound: Dolby Atmos can also make the sound clearer and more detailed. This can be especially beneficial for music, as it can help you to hear all of the nuances of the instrumentation and vocals. Wider soundstage: Dolby Atmos can create a wider soundstage, which means that you can hear sounds coming from all around you. This can make you feel like you are surrounded by sound. More realistic sound effects: Dol...