Lottie animation is showing as a still image but not playing

Viewed 3878

I am in the process of writing an app and wanted to have a Lottie animation as my splash screen, since I am learning java as I code this app I made a test app to see how things would work. I found that almost any Lottie animation displays just as a still image and does not play/loop the animation. I followed the guide on LottieFiles website and also information that I found on other questions but I still did not manage to get the animation playing. The following is exactly what I have added:

Gradle dependency of my app:

dependencies {  
        implementation 'com.airbnb.android:lottie:3.6.1'
}

To my Layout XML:

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/splashlottie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_fileName="opening_book.json" //Even tried placing the file in raw and using app:lottie_rawRes="@raw/opening_book"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

In my activity:

        LottieAnimationView splashscreen;
        splashscreen = findViewById(R.id.splashlottie);

        splashscreen.animate();
        splashscreen.playAnimation();

I am not sure exactly what I am doing wrong as I even tried various different lottie animation files and by placing the same file under raw and assets. Could the API level be the cause of the animation not playing?

3 Answers

I was able to run the animation with the following code:

val imageView = view.findViewById<LottieAnimationView>(R.id.image_view)
imagView.playAnimation()

xml code:

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:lottie_fileName="womensDay.json"
    app:layout_constraintBottom_toTopOf="@+id/button_first"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textview_first"
    app:lottie_loop="true"/>

A few things to remember:

  1. Make sure you're placing your json file in the asset folder, if don't have one create one. Refer: https://stackoverflow.com/a/50907775/5745574
  2. Just to test, try giving your view absolute dimension say 200dp (width & height). It might be a case where your view doesn't have proper dimension.
  3. Try animating a different json file, your file may be corrupted
  4. Try placing your lottieView inside a ViewGroup (Of any type e.g. LinearLayout)
  5. Lottie animation is supported in API level 16 and above

This is work without lottieView.playAnimation() because lottie_autoPlay="true":

<com.airbnb.lottie.LottieAnimationView
            android:id="@+id/anim_view"
            android:layout_width="256dp"
            android:layout_height="256dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:lottie_rawRes="@raw/anim_radio_waves"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"/>

Also check your animation json: lottie player

Related