Search code examples
androidkotlincolorslottie

set color for Lottie animation programatically


I am using below code to change the animation color specially for setting the text color but not succeed. Thanks in advance for the help

   //UI xml code

     <com.airbnb.lottie.LottieAnimationView
      android:id="@+id/lottie_count_down"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginStart="@dimen/dp_0"
      android:translationZ="@dimen/dp_20"
      android:visibility="visible"
      app:lottie_rawRes="@raw/countdown_animation" /> // .JSON file

// Kotlin Code

    val simpleColor = SimpleColorFilter(Color.parseColor(App.settings!!.theme.textColor))
    val callback = LottieValueCallback<ColorFilter>(simpleColor)
    callback.setValue(simpleColor)

    lottieCountDownAnimation.addValueCallback(
        KeyPath("**"),
        LottieProperty.COLOR_FILTER,
        callback
    ) // issue in setting text color 

    lottieCountDownAnimation!!.setBackgroundColor(Color.parseColor(App.settings!!.theme.backgroundColor)) 
    // works for setting the background color

    lottieCountDownAnimation.playAnimation()

Solution

  • var mCallback: LottieValueCallback<Int?>
    lottieCountDownAnimation.addLottieOnCompositionLoadedListener(
        LottieOnCompositionLoadedListener {
            mCallback = LottieValueCallback<Int?>()
        
    mCallback.setValue(Color.parseColor(App.settings!!.theme.textColor))
            lottieCountDownAnimation.addValueCallback(
                KeyPath("**"),
                LottieProperty.COLOR,
                mCallback
            )
        })