How to get a resource from string in Kotlin?

Viewed 392

Can you get a resource from a string like this in Kotlin?

val diceImage: ImageView = findViewById(R.id.imageView)
val diceRoll = (1..6).random()
diceImage.setImageResource("R.drawable.dice_${i}")
1 Answers

You can use the getIdentifier method:

val id  = resources.getIdentifier("resourceName", "drawable", packageName)
imageView.setImageResource(id)
Related