In my Android project, I want to loop through the entire collection of Drawable
resources. Normally, you can only retrieve a specific resource via its ID using something like:
InputStream is = Resources.getSystem().openRawResource(resourceId)
However, I want to get all Drawable
resources where I won't know their ID's beforehand. Is there a collection I can loop through or perhaps a way to get the list of resource ID's given the resources in my project?
Or, is there a way for me in Java to extract all property values from the R.drawable
static class?
Add a picture named aaaa and another named zzzz, then iterate through the following:
This worked for me.
Just do this:
The OP wanted drawables and I needed layouts. This is what I came up with for layouts. The
name.startsWith
business lets me ignore system generated layouts, so you may need to tweak that a bit. This should work for any resource type by modifying the value ofclz
.If you find yourself wanting to do this you're probably misusing the resource system. Take a look at assets and
AssetManager
if you want to iterate over files included in your .apk.i used getResources().getIdentifier to scan through sequentially named images in my resource folders. to be on a safe side, I decided to cache image ids when activity is created first time:
Okay, this feels a bit hack-ish, but this is what I came up with via Reflection. (Note that
resources
is an instance of classandroid.content.res.Resources
.)If anyone has a better solution that makes better use of Android calls I might not be aware of, I'd definitely like to see them!