0

In the res/drawable-mdpi folder I have 26 letter images, named big_0.png to big_25.png.

I would like to add them all (and ignore any other images in the folder) to a hash map:

private static HashMap<Character, Drawable> sHash = 
    new HashMap<Character, Drawable>();

Initially I was planning something like:

private static final CharacterIterator ABC = 
    new StringCharacterIterator("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

int i = 0;
for (char c = ABC.first(); 
    c != CharacterIterator.DONE; 
    c = ABC.next(), i++) {

        String fileName = "R.drawable.big_" + i;
        Drawable image = context.getResources().getDrawable(fileName);
        sHash.put(c, image);
}

But then I've realized that R.drawable.big_0 to R.drawable.big_25 are of type int and not String.

So please advise me, how to iterate through the 26 images correctly?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

1 Answers1

4

Use getResources().getIdentifier() to convert "R.drawable.big_" + i into the resource ID value that you would use with getDrawable().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    `int id = context.getResources().getIdentifier("big_" + i,"drawable", context.getPackageName());` has worked for me, thanks. (And note to myself: when you forget to call `setBounds()` for the drawables - they are not visible). – Alexander Farber Nov 15 '14 at 18:24