Accessing contents of R.string using a variable to represent the resource name
private String getStringResourceByName(String aString)
{
String packageName = context.getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
You can use the method to convert string into int identifier:
public static int getStringIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "string", context.getPackageName());
}
Pass in an activity as context parameter (or any other Context instance). Then you can use the identifier as usual with getString() method.
Note that conversion from string to identifier uses reflection and thus can be not that fast, so use carefully.
As a Kotlin extension, ArK's answer could be written the following way. It also catches an exception when you give it an unknown key.
fun Context.getStringWithResKey(nameResKey: String): String {
val resId = resources.getIdentifier(nameResKey, "string", packageName)
return try {
getString(resId)
} catch (e: Exception) {
Log.e(this.javaClass.simpleName, "Couldn't find string value for key '$nameResKey'", e)
""
}
}
Another thing you can do is map the string to the resource. That way you can have the string in a variable (or build it programmatically) and then use the map to look up the resource id.
Example
strings.xml
<string name="water_english">water</string>
<string name="water_spanish">agua</string>
<string name="water_chinese">水</string>
<string name="water_mongolian">ᠤᠰᠥ</string>
Code
public class MainActivity extends AppCompatActivity {
private Map<String, Integer> stringForResourceIdMap = createMap();
private Map<String, Integer> createMap() {
Map<String, Integer> result = new HashMap<>();
result.put("water_english", R.string.water_english);
result.put("water_spanish", R.string.water_spanish);
result.put("water_chinese", R.string.water_chinese);
result.put("water_mongolian", R.string.water_mongolian);
return result;
}
private String getMyStringResource(String lookupString) {
int resourceId = stringForResourceIdMap.get(lookupString); // R.string.xxx
return getString(resourceId);
}
// ...
}
Note
If you are localizing your app with different translations, then you should be using different resource folders.