user6951371
user6951371

Reputation: 13

Android drawable images int

I am using large number of images in drawable folder And i want to get it all in

int[] images = new int[] {
R.drawable.pic4
R.drawable.pic3
R.drawable.pic2
};

Is there any way to get all without writing the names of images one by one?

Upvotes: 1

Views: 1179

Answers (3)

zombie
zombie

Reputation: 5259

i think you've done a good job naming the image pic2, pic3.....

this might help a lot. you can write a while loop and keep on going until you end up with a name that doesn't have any resource file

this class would get the drawable for you

    public class ImageHelper
{
    public static int getImageId(Context context, String name, String param)
    {
        try
        {
            name = param == null ? name : (name  + param.toLowerCase());
            int res = getResId(context, name, R.drawable.class);
            if (res == 0)
            {
                Log.d("ImageHelper", "missing drawable: " + name);
            }
            return res;
        } catch (Exception e)
        {
            return 0;
        }
    }

    public static int getResId(Context context, String resName, Class<?> c)
    {
        try
        {
            return context.getResources().getIdentifier(resName, c.getSimpleName(), context.getPackageName());
        } catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }
}

use it like this

String imageName = "pic";
int index = 0; // or any index you want to start with

ArrayList<Integer> images = new ArrayList<>();
while (true) {
    int resourceId = ImageHelper.getImageId(context, imageName, "" + index);
    index++;
    if (resourceId > 0){
        images.add(resourceId);
    } else {
        break;
    }
}

Upvotes: 0

Ajith Pandian
Ajith Pandian

Reputation: 1342

Name your Drawables like pic1,pic2,pic3.....picN.

Then you can get those Drawables by,

    int [] drawables=new int[N];
    for (int i = 1; i < N; i++) {
        drawables[i] = getResources()
                .getIdentifier("pic"+i, "drawable", getPackageName()));
    }

I hope it will help you. All the best.

Upvotes: 1

Matei Radu
Matei Radu

Reputation: 2078

It is possible to store the listing of your drawables in an XML array. This is not an exact solution to your problem but at least it can result in a cleaner Java code.

res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="my_drawables">
        <item>@drawable/image1</item>
        <item>@drawable/image2</item>
        <item>@drawable/image3</item>
        <!-- Add other drawables -->
    </array>
</resources>

In your code

final TypedArray drawableArray = getResources()
    .obtainTypedArray(R.array.my_drawables);

// Example of using the array.
// Parameters are as follow: getResourceId(int index, int defValue)
// and it provides the int value of the desired drawable.
myImageView.setImageResource(drawableArray.getResourceId(1, -1));

This implementation still requires to write once the list of drawables but you eliminate possible errors cause by hardcoded strings in for cycles and it's easy to use across your code.

Upvotes: 0

Related Questions