I would like to store drawable resources' ID in the form of R.drawable.* inside an array using an XML values file, and then retrieve the array in my activity.
Any ideas of how to achieve this?
I would like to store drawable resources' ID in the form of R.drawable.* inside an array using an XML values file, and then retrieve the array in my activity.
Any ideas of how to achieve this?
In Kotlin, you can do as:-
<integer-array name="drawer_icons">
<item>@drawable/drawer_home</item>
</integer-array>
You will get array of Image from the resource as TypedArray
val imageArray = resources.obtainTypedArray(R.array.drawer_icons)
get resource ID by the index
imageArray.getResourceId(imageArray.getIndex(0),-1)
OR you can set imageView's resource to the id
imageView.setImageResource(imageArray.getResourceId(imageArray.getIndex(0),-1))
and in last recycle the array
imageArray.recycle()
kotlin way could be this:
fun Int.resDrawableArray(context: Context, index: Int, block: (drawableResId: Int) -> Unit) {
val array = context.resources.obtainTypedArray(this)
block(array.getResourceId(index, -1))
array.recycle()
}
R.array.random_imgs.resDrawableArray(context, 0) {
mImgView1.setImageResource(it)
}