1

I'm making an Android application and it's got a lot of images (about 200). And I want to show the specific image according to what the user selects.

The selections are in the form of Strings such as "USD", "AUD", "LKR", etc. And the images are in the form "usd.png", "aud.png", "lkr.png" and so on. I have put the images in the /res/drawable-mdpi folder and I usually access them like R.drawable.usd and so on.

But in this case its [obviously] hard to do that (for 200+ icons). So is there anyway I can obtain the specific image according to user selection? For example, when user selects "USD", I want to obtain the image named "usd.png". Any help?

Thanks in advance.

5 Answers 5

2

Could you use a combination of Resources.getIdentifier and Resources.getDrawable ?

http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)

http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your answer! I'll do it this way.. I wasn't aware there was a getIdentifier() method.. :)
2

If you can construct the name of the resource, you can call getResources().getIdentifier() to get the id, then you can take it from there.

3 Comments

On what should I call getIdentifier?
Sorry, I should have said getResources().getIdentifier().
Thanks for the answer! I'll do it this way, but I'm choosing the other answer (that said the same thing) only because it has some links. Nevertheless, thanks for your answer! +1
0

Use a Map for storing the String as a key, with the image name as a value. For example:

Map<String, String> images = new HashMap<String, String>();
images.put("USD", "usd.png");        // store the reference
String usdImage = images.get("USD"); // retrieve the reference

5 Comments

Tried that, but I have to store 200+ values like that :) So I was looking for more easier and efficient way of doing it..
200 values is nothing, really! you'll be fine using a Map for that.
I'll look at reflection as the other answerer said, and if its not working as expected, I'll resort to this.. Thanks for the answer!
Refelection can be a very expensive option in terms of performance. Don't reinvent the wheel, what you're asking for is the textbook example of when to use a Map data structure
Some other answerers said to use the Resources.getIdentifier() method in the Android SDK.. If that works, then that's the best method as its a whole lot more efficient and performance-friendly than doing this manually. Another thing, if I add more images and Strings in the future, I wouldn't have to remember to add all of them to the Map :)
0

I'm not sure if Android has methods to access the resources by name, however you can always use reflection to do such things in Java:

R.drawable.getClass().getField("nameGoesHere").get(null)  

1 Comment

Sorry, I should have said getResources().getIdentifier(). I have edited my answer.
0

The Reflection API is working on Android. It's something that all Java Developer should learn.

2 Comments

Fairly general answer, how exactly would you do that? There's better ways of getting resources by using the Android SDK
It was my intention to not write a complete answer, to point the utility of the full API, and not just give a quick answer. Learn fishing instead of giving a fish...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.