Accessing contents of R.string using a variable to represent the resource name

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.

Leave a Comment