I’m trying to use a random number generator to pick between one of 10 different animations to run using a Lottie implementation. I’ve named the animations animation1 to animation10. When directly inputing one of the animations like this there’s no issue:
animationView.setAnimation(R.raw.animation2);
but the app keeps on crashing when inputing it like this:
LottieAnimationView animationView = findViewById(R.id.animationViewer);
randNumber = rand.nextInt(10) + 1;
animationView.setAnimation("R.raw.animation" + randNumber);
With the cause being:
Caused by: java.io.FileNotFoundException: R.raw.animation2
3
Answers
Found this to be the best solution
You can’t do that because
setAnimation()
requires a ResID and not a string.Instead, you can use an
Array
like thisAnd then, you can use your random number.
you are trying to send a resource id as a string
you can create an array of resources or the easiest but not the cleanest way is :
1 -> {R.raw.animation1}
2 -> {R.raw.animation2}
.
.
.
.
10 -> {R.raw.animation10}
}
animationView.setAnimation(animID);