Issue
In Android, I have only found answers as to how to open a single specific Drawable
from MainActivity.java
, but not how to iterate over each Drawable
from res/drawables
. The Drawable
s names do not follow any patterns (e.g. being numbered from 0 to 25), so the answer suggested here sadly doesn't solve my problem. Does anyone know how to do the latter?
Thank you in advance :)
Solution
First, put your drawables into an arrays
<array name="dashboard_item_menu_drawable">
<item>@drawable/ic_file_green</item>
<item>@drawable/ic_email_green</item>
<item>@drawable/ic_linear_scale_green</item>
<item>@drawable/ic_undo_green</item>
<item>@drawable/ic_check_circle_green</item>
<item>@drawable/ic_archive_green</item>
</array>
Then, iterate your array drawables
val icons = ArrayList<Int>()
val arr = resources.obtainTypedArray(R.array.dashboard_item_menu_drawable)
(0 until arr.length()).forEach {
// get resource id of each drawable
val icon = arr.getResourceId(it, -1)
icons.add(icon)
}
Next, recycles resource
arr.recycle()
Then you can use your drawable
iconView.setImageDrawable(ContextCompat.getDrawable(this, icons[index]))
Answered By - M Moersalin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.