Referencing Resource Via String


Last Updated:

  1. safibaba

    safibaba Member This Topic's Starter

    Joined:
    May 3, 2010
    Messages:
    15
    Likes Received:
    0
    I was just wondering if there is any way to reference a resource using a string.

    Say you had 10 images in your drawable folder called 'img1' 'img2' 'img3' etc......

    PHP:
    1. int imgNum = 2;
    2. String imgName = "img"+imgNum;
    3. Drawable d = (Drawable) getResources().getDrawable(R.drawable.imgName);
    Is there any way of doing this? Or will I just have to use if statements to work out which image to use?
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi,

    It might be overkill, but if you want something that will scale up to handle lots of resources then you could use Java reflection.

    I describe a solution in another thread:

    http://androidforums.com/android-de...lements-res-raw-folder-one-shot-run-time.html

    You'd have to adapt the code a little. Instead of:

    Code (Text):
    1. Field fields[] = R.raw.class.getDeclaredFields() ;
    2.  
    You'd have to use:

    Code (Text):
    1. Field fields[] = R.raw.drawable.getDeclaredFields() ;
    2.  
    If you're familiar with Java reflection then hopefully that other thread will give you an idea of how to achieve what you want.

    I can explain further if you like. Just ask.

    Regards,

    Mark
     
  3. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    To answer your first question: no, I don't know a way to reference your drawables by a String.

    To answer your second question: I use Maps to handle problems like yours where I know there are going to be multiple statically paired items.

    For example, I might declare the global variable:
    Code (Text):
    1.  
    2. public static final String PREFIX = "img";
    3. public static final Map mImageMap = new HashMap<String, Drawable>();
    4.  
    ...then, in onCreate I'd pay the one time setup fee by putting:
    Code (Text):
    1.  
    2. if (mImageMap.size() == 0) {
    3.    mImageMap.put(PREFIX + "1", getResources().getDrawable(R.drawable.img1));
    4.    mImageMap.put(PREFIX + "2", getResources().getDrawable(R.drawable.img2));
    5.    mImageMap.put(PREFIX + "3", getResources().getDrawable(R.drawable.img3));
    6. }
    7.  
    and then finally when I want to reference the value, I would do something like:
    Code (Text):
    1.  
    2. int exampleInput = 2;
    3. Drawable d = mImageMap.get(PREFIX + exampleInput);
    4.  
     
    nguyendat likes this.
  4. safibaba

    safibaba Member This Topic's Starter

    Joined:
    May 3, 2010
    Messages:
    15
    Likes Received:
    0
    Ahh - hashmap - that's a great idea!

    I'm so used to working a a language where this is possible, I didn't even think of making a dictionary / hash.

    Thanks Boogs. I actually just wrote a factory method to return the relevant image, since there were only three, but I will need to do one with much more images soon, so this will be a great solution, and much easier to maintain.

    Cheers:):)
     

Share This Page

Loading...