A common requirement in animation-heavy apps is the ability to swap between different Lottie JSON files without restarting the activity. This demo implements a straightforward toggle pattern: a single Change button cycles betweenDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/lottie-kt-test/llms.txt
Use this file to discover all available pages before exploring further.
wh.json and logo2.json by tracking the current animation name in a local variable and reassigning it on each tap.
Button Click Handler
The complete click listener fromMainActivity.kt:
MainActivity.kt
How It Works
The when Toggle Expression
The when expression evaluates the current value of anim and returns the next animation filename:
- If
animis"wh.json", it switches to"logo2.json". - If
animis"logo2.json", it switches back to"wh.json". - The
elsebranch guards against any unexpected state by defaulting back to"wh.json".
anim, so successive taps continue alternating correctly.
Why lottieView.frame = 0 Is Needed
After calling setAnimation() with a new file, the LottieAnimationView has loaded the new composition but the internal frame counter still holds whatever position the previous animation stopped at. Setting lottieView.frame = 0 explicitly resets the playhead to the very first frame of the new animation before playAnimation() is called. Without this reset, the new animation would start partway through — or not appear to start at all if the frame counter exceeds the new composition’s total frame count.
Sequence of Calls
The three calls must happen in this order:setAnimation(anim)
Loads and parses the new Lottie JSON composition from the
assets/ directory. This replaces the previously loaded composition in memory.frame = 0
Resets the playhead to frame zero so playback begins from the start of the new composition.
Next, learn how to replace embedded images inside a Lottie JSON with your own Android drawables: Image Asset Delegation