How to Display List of Resource Drawables

How to display list of resource drawables

Using the getFields method on the drawable class, you are able to iterate through the entire list of drawables.

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
try {
System.out.println("R.drawable." + f.getName());
} catch (Exception e) {
e.printStackTrace();
}
}

Reference: http://www.darshancomputing.com/android/1.5-drawables.html

Android - Getting a list of drawable resource

well, if you know the suffix of the images, you can request the identifier for a drawable by getResources().getIdentifier(...) and then using the identifier get the drawable. So if you know how many images you have, then you can create a loop and store each of the drawables in a list. Just take into account that such a lookup is relatively expensive.

Retrieving all Drawable resources from Resources object

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.

How to get all Drawable resources?

To iterate through your drawables, you can use the Field class.

Try something like the following:

Field[] drawablesFields = com.your.project.R.drawable.class.getFields();
ArrayList<Drawable> drawables = new ArrayList<>();

for (Field field : drawablesFields) {
try {
Log.i("LOG_TAG", "com.your.project.R.drawable." + field.getName());
drawables.add(getResources().getDrawable(field.getInt(null)));
} catch (Exception e) {
e.printStackTrace();
}
}

Or you could also use the assets folder. Put your images inside a sub-directory called images, and the use the AssetManager class to get those files. This is an approach that is not project-specific.

Try something like:

AssetManager am = context.getAssets();
String[] files = am.list("images");
InputStream istr = null;
ArrayList<Drawable> drawables = new ArrayList<>();

for (String file : files) {
Drawable d = Drawable.createFromStream(am.open(file), null);
drawables.add(d);
}

How to get list of images from res folder in android

Use this,

Field[] list = R.drawable.class.getFields();
BitmapDrawable[] drawables=new BitmapDrawable[list.length];
for (int i=0;i<list.length;i++){
int id=list[i].getInt(null);
try{
drawables[i]=(BitmapDrawable)getResources().getDrawable(id);
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Hope this helps!!!!!!!!

how to access the drawable resources by name in android

Note that your approach is almost always the wrong way to do things (better to pass the context into the object itself that is using the drawable than keeping a static Context somewhere).

Given that, if you want to do dynamic drawable loading, you can use getIdentifier:

Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);

Get array of id drawable resources

If I understand your question.. here is your answer:

try this:
You can use typed array..

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="random_imgs">
<item>@drawable/car_01</item>
<item>@drawable/balloon_random_02</item>
<item>@drawable/dog_03</item>
</string-array>

</resources>

Then in your activity access them like so:

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
//get resourceid by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));

how to select from resources randomly (R.drawable.xxxx)

You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme.

You have to map the name to the identifier using the getIdentifier method of the Resources class.

String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");

The documentation for this method says:

Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.

This is true but need not be a problem if you are doing it in code that isn't performance sensitive.

Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.



Related Topics



Leave a reply



Submit