15

Possible Duplicate:
Dynamic Resource Loading Android

In Android, I can load a string from the resources with String s = getString(R.string.keyName). But I have a list of categories in my database, each one which has a name. How can I take that category name and then load the appropriate string resource based on it, so that it will work for localization?

Basically, I need to have the keyName be dynamic; my own String variable. Is this possible? Thanks.

3
  • the localization part is done with a lookup in say res.values (default), res.values-en-rCA (Canada) res.values-en-rGB (United Kingdom) etc. If the string is not present in the localized language for the user, it is fetched from res.values. Commented May 27, 2011 at 21:40
  • try this: String value = view.getResources().getString(R.string.class.getField("stringName").getInt(null)); Commented Sep 14, 2016 at 21:08
  • 1
    int asd = getResources().getIdentifier("MYDYNAMICKEY","string",mContext.getPackageName()); String bla = getString(asd); Commented Dec 2, 2016 at 14:42

2 Answers 2

11

As your resources cannot be dynamically, you could use a switch statement like:

String name = "";
switch (row.getNameKey()) {
case keyName1:
    name = getString(R.string.keyName1);
    break;
case keyName2:
    name = ...
    ...
}

Another approach woould be the getIdentifier method: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

and see: Android: Accessing string.xml using variable name

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

9 Comments

How is that giant switch statement different than name = getString(row.getNameKey())?
I'm doing that in one place right now; a place that only has 4 options. But the categories are dynamic and database-driven; there could be hundreds of values. Of course I'd still have to list them all out in the resource file, but I figure there must be some way to not have to switch it in my code like that...
@Sam - getNameKey()? That sounds like what I would want; but I can't find a method like that....
@Sam: I edited it like it was ment to be not the String id in the case statement. @Gendolkari: getNameKey is YOUR method where you get the key from the db. So just save the strings-id in db. But this is a mess once if you change the ids.
I added another idea to my answer. Maybe this is what you are looking for?
|
4

You can use Java Reflection to turn the string into the resource ID. If you know in advance that it's a string, say R.string.theName, and you have a keyname of "theName", you just need to use reflection on "your.package.com.R.string" (where "your.package.com" is the package name defined in AndroidManifest.xml) to find the class, then use reflection to get the "theName" static member from it. The value you receive can be passed into the getString() method.

4 Comments

This is the best answer, but it will be highly inefficient. You can find numerous samples explaining reflection in Java. Just be very careful with how often you make use of this method.
This isn't the best answer, as the Android SDK has methods specifically for looking up resources via name - no need to use reflection.
@ZoFreX please be specific in your answer - which methods do you mean?
Hi @LeosLiterak! The top answer on this page by "Stuck" briefly mentions the answer - it's getidentifier. There's example code if you follow the link at the top of this page where it says "Possible Duplicate: Dynamic Resource Loading Android". Tl;dr: Resources#getIdentifier

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.