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.

Community
  • 1
  • 1
GendoIkari
  • 11,734
  • 6
  • 62
  • 104
  • 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. – JAL May 27 '11 at 21:40
  • try this: String value = view.getResources().getString(R.string.class.getField("stringName").getInt(null)); – Jeba Ranganathan Sep 14 '16 at 21:08
  • 1
    int asd = getResources().getIdentifier("MYDYNAMICKEY","string",mContext.getPackageName()); String bla = getString(asd); – Maurice Raguse Dec 02 '16 at 14:42

2 Answers2

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

Community
  • 1
  • 1
Stuck
  • 11,225
  • 11
  • 59
  • 104
  • How is that giant switch statement different than `name = getString(row.getNameKey())`? – Sam May 27 '11 at 21:37
  • 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... – GendoIkari May 27 '11 at 21:39
  • @Sam - getNameKey()? That sounds like what I would want; but I can't find a method like that.... – GendoIkari May 27 '11 at 21:40
  • @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. – Stuck May 27 '11 at 21:45
  • I added another idea to my answer. Maybe this is what you are looking for? – Stuck May 27 '11 at 21:47
  • Thanks, the getIdentifier was exactly what I was looking for! – GendoIkari May 31 '11 at 16:13
  • Thanks. You pointed in the right direction with the first link. Here's a blog link for the code I found http://steven.bitsetters.com/2007/11/27/accessing-android-resources-by-name-at-runtime/ – Sufian Nov 01 '13 at 10:41
  • what about if i have 100+ rows in my List? i can't write Switch with 100+ cases – Jimale Abdi Sep 19 '19 at 20:22
  • Thank you i get solution for the links you provided – Jimale Abdi Sep 19 '19 at 20:23
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.

mah
  • 39,056
  • 9
  • 76
  • 93
  • 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. – Sam May 28 '11 at 15:10
  • 1
    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 Feb 13 '14 at 11:26
  • @ZoFreX please be specific in your answer - which methods do you mean? – Leos Literak Apr 23 '16 at 18:57
  • 1
    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 – ZoFreX Apr 24 '16 at 14:08