Den
Den

Reputation: 47

Images management, array of images

I have 35 images named from pic1.png to pic35.png in my res\drawable folder. I need to create array of this pics to fill my ListView. I know I need some loop.

int[] img;
for (int i = 1; i <= 35; i++) {
            //here should be my loop body, but i dunno :( i tried this, but ofc this is wrong.
            img = R.drawable.pic[i];
        }

One more question: is there any chance to manage my images in drawable folder? I mean, if I have two packs of images, I can't do res\drawable\pack1 and res\drawable\pack2 folders, is the only way to manage images to give em names like pack1_pic1 etc?

Upvotes: 0

Views: 44

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

Answer to your first question:

you can set image id to your int array using:

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

which would return the value of R.drawable.pic1...35;

Regarding your second question

No, the resources mechanism doesn't support subfolders in the drawable directory,

see this: Answer

Upvotes: 1

Karthik CP
Karthik CP

Reputation: 1200

Try this

for (int i = 1; i <= 35; i++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("pic"+i, "drawable", getPackageName()));
}

Upvotes: 1

Related Questions