Retrieving All Drawable Resources from Resources Object

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 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

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);

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.

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 do you obtain a Drawable object from a resource id in android package?


Drawable d = getResources().getDrawable(android.R.drawable.ic_dialog_email);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(d);

Get all drawables base on file prefix and store it on Array of Int

You should use getDeclaredFields() rather than getFields and also you should use the drawableResources as

final Field[] fields =  R.drawable.class.getDeclaredFields();
final R.drawable drawableResources = new R.drawable();
List<Integer> lipsList = new ArrayList<>();
int resId;
for (int i = 0; i < fields.length; i++) {
try {
if (fields[i].getName().contains("l_1_")){
resId = fields[i].getInt(drawableResources);
lipsList.add(resId);
}
} catch (Exception e) {
continue;
}
}

Taken from here

The better approach is store resources related to the list in the integer-array in your arrays.xml or strings.xml file like below

<integer-array name="images_for_my_list">
<item>@drawable/abc_menu_hardkey_panel_mtrl_mult</item>
<item>@drawable/mylist_img_2</item>
<item>@drawable/abc_menu_hardkey_panel_mtrl_mult</item>
<item>@drawable/mylist_img_3</item>
</integer-array>

And use it as

    TypedArray tArray = mcontext.getResources().obtainTypedArray(
R.array.images_for_list);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
ids[i] = tArray.getResourceId(i, 0);
// How to Use it
// setImageResource(ids[position]);
// Or
// Drawable drawable = ContextCompat.getDrawable(mContext.getApplicationContext(),ids[position])
// imageView.setImageDrawable(drawable);
}

Get drawable from other resource folder


The folder "coffee_types" is an Android resource folder with type drawable

No, it is not. You cannot invent your own resource types.



Related Topics



Leave a reply



Submit