Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to add Lottie Animation in an Android app

Lottie is a library from Airbnb that allows you to integrate animations created in Adobe After Effects into your Android, iOS, and React Native apps. These animations are exported as JSON files using the Bodymovin extension.

To add a Lottie animation to your Android app, follow these steps:

  1. Add Dependencies:

    Open your app's build.gradle file and add the following dependencies:

    implementation 'com.airbnb.android:lottie:4.1.0' // Check for the latest version
    
  2. Add Lottie Animation to Assets or raw:

    Place your Lottie JSON file (e.g., animation.json) in the assets folder or res/raw directory of your app.

  3. Using Lottie Animation in XML Layout:

    Add the LottieAnimationView to your XML layout:

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        app:lottie_fileName="animation.json" /> <!-- If you placed it in assets -->
        <!-- or use app:lottie_rawRes="@raw/animation" if you placed it in res/raw -->
    

    Here:

    • lottie_autoPlay: Determines whether the animation should automatically start playing as soon as it��s loaded or the view is attached.
    • lottie_loop: Determines whether the animation should loop.
    • lottie_fileName: The file name of the animation in the assets folder.
    • lottie_rawRes: Use this if you've placed the JSON file in res/raw.
  4. Using Lottie Animation Programmatically:

    In your activity or fragment:

    LottieAnimationView animationView = findViewById(R.id.animation_view);
    animationView.setAnimation("animation.json"); // If you placed it in assets
    // or use animationView.setAnimation(R.raw.animation); if you placed it in res/raw
    animationView.playAnimation();
    
  5. Additional Controls:

    Lottie provides a variety of controls like:

    • animationView.pauseAnimation(): To pause the animation.
    • animationView.resumeAnimation(): To resume the animation.
    • animationView.setProgress(0.5f): To set the animation progress (0.0 to 1.0).
    • And more��
  6. Listening for Animation Updates:

    You can set an animation listener if you want to get callbacks for start, end, etc.:

    animationView.addAnimatorListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            // Animation started
        }
    
        @Override
        public void onAnimationEnd(Animator animation) {
            // Animation ended
        }
    
        @Override
        public void onAnimationCancel(Animator animation) {
            // Animation canceled
        }
    
        @Override
        public void onAnimationRepeat(Animator animation) {
            // Animation repeated
        }
    });
    
  7. Run your app:

    Once you've set up everything, run your app to see the Lottie animation in action.

  1. Integrating Lottie Animation in Android app:

    • Description: Integrate Lottie Animation library into your Android app to display rich and dynamic animations.

    • Code:

      // Add the Lottie library dependency in your app-level build.gradle file
      implementation 'com.airbnb.android:lottie:4.1.0'
      
      <!-- Include LottieAnimationView in your layout file -->
      <com.airbnb.lottie.LottieAnimationView
          android:id="@+id/lottieAnimationView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:lottie_fileName="your_animation.json"
          app:lottie_autoPlay="true"
          app:lottie_loop="true" />
      
  2. Customizing Lottie Animation playback in Android:

    • Description: Customize Lottie Animation playback options, such as play, pause, loop, and speed.
    • Code:
      // Reference to LottieAnimationView
      LottieAnimationView animationView = findViewById(R.id.lottieAnimationView);
      
      // Play animation
      animationView.playAnimation();
      
      // Pause animation
      animationView.pauseAnimation();
      
      // Set animation speed
      animationView.setSpeed(1.5f);
      
      // Set loop
      animationView.setRepeatCount(LottieDrawable.INFINITE);
      
  3. Handling Lottie Animation events and interactions:

    • Description: Handle events and interactions in Lottie Animation, such as completion events.
    • Code:
      // Set up animation listener
      animationView.addAnimatorListener(new Animator.AnimatorListener() {
          @Override
          public void onAnimationStart(Animator animation) {
              // Animation start
          }
      
          @Override
          public void onAnimationEnd(Animator animation) {
              // Animation end
          }
      
          @Override
          public void onAnimationCancel(Animator animation) {
              // Animation canceled
          }
      
          @Override
          public void onAnimationRepeat(Animator animation) {
              // Animation repeated
          }
      });
      
  4. Implementing Lottie Animation with RecyclerView items:

    • Description: Apply Lottie Animation to individual items within a RecyclerView for a dynamic and engaging user interface.
    • Code:
      // In your RecyclerView adapter's onBindViewHolder
      LottieAnimationView animationView = holder.itemView.findViewById(R.id.lottieAnimationView);
      animationView.setAnimation("your_animation.json");
      animationView.playAnimation();
      
  5. Using Lottie with XML layout files in Android:

    • Description: Embed LottieAnimationView directly in XML layout files for a clean and declarative design.
    • Code:
      <!-- Include LottieAnimationView in your layout file -->
      <com.airbnb.lottie.LottieAnimationView
          android:id="@+id/lottieAnimationView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:lottie_autoPlay="true"
          app:lottie_loop="true"
          app:lottie_fileName="your_animation.json" />