-1

I'm sorry I need to keep asking this as I'm not getting any answers.

In Android Studio you can set a drawable as the splash screen like so:

splash_screen.xml in the drawable folder:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/blue"/>
    <item
        android:drawable="@mipmap/ic_launcher_foreground"
        android:gravity="center"
        android:top="201dp"
        android:bottom="201dp"
        android:right="100dp"
        android:left="100dp"/>
</layer-list>

Add SplashTheme to styles.xml:

<style name="SplashTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:background">@drawable/splash_screen</item>
    <item name="android:windowAnimationStyle">@null</item>
</style>

SplashActivity class (doesn't have a layout):

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

And finally in Manifest remove the intent-filter from MainActivity and add it to SplashActivity:

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".MainActivity" />

And that's it. So instead of a drawable how do I add a lottie animation? I know it's possible because I've seen many apps load an animation immediately when opened. I've googled this question and the answers provided are all wrong because they instruct you to add a new launcher activity that contains a layout with the animation that plays it till the end or uses handler.postDelayed. The goal of the animation is supposed to be showing the user something while the app loads. Why would you intentionally add a delay for the user that's not part of the loading time?

  • 2
    Read what you typed: "The goal of the animation is supposed to be showing the user something while the app loads.", and who's going to load the requirements needed to play an animation? Lottie is an external library, and as such, it must be loaded before it can function. The Android Entry Point is an Application class, but that's not the UI, that's the process starting, so you still need to load your application before your UI is ready, and even then the View system needs to launch the "Launcher Activity" and all it entails. So you cannot do what you're trying to do as far as I know. – Martin Marconcini Jul 26 '21 at 09:04

1 Answers1

1

I use Lottie animation like this,

XML

    <com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animationView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    app:lottie_url="https://assets6.lottiefiles.com/packages/lf20_Z5qhQy.json"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"/>

Java

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(SplashActivity.this , HomeActivity.class));
            finish();
        }
    },5000);

Dependency

implementation 'com.airbnb.android:lottie:3.7.0'

Manifest

<uses-permission android:name="android.permission.INTERNET" />

<activity android:name=".SplashActivity"
        android:noHistory="true">

Output

output

Get more animation click here

Ganesh MB
  • 1,109
  • 2
  • 14
  • 27