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.
Android: Accessing string.xml using variable name
See Resources.getIdentifier
: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29 . You can try something like this:
public void showPrompt(Prompt prompt, String label) {
String message = (String) getResources().getText(getResources().getIdentifier(label, "string", null));
//show a dialog box with message
}
Try that out and see what that does for you.
EDIT: meh. Try this instead.
public void showPrompt(Prompt prompt, String label) {
String message = (String) getResources().getText(getResources().getIdentifier(label, "string", "<application package class>"));
//show a dialog box with message
}
Turns out you have to specify the your package identifier (so if your AndroidManifest.xml has com.blah.blah.blah as the Package put that in the third parameter.
Android development: how to access a string when the ID is in a variable?
You can use this
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
thing.setText(getStringResourceByName("question1"));
Or simply the one liner
getResources().getString(getResources().getIdentifier("question1", "string", getPackageName()))
Android: How do I get string from resources using its name?
The link you are referring to seems to work with strings generated at runtime. The strings from strings.xml are not created at runtime.
You can get them via
String mystring = getResources().getString(R.string.mystring);
getResources()
is a method of the Context
class. If you are inside a Activity
or a Service
(which extend Context) you can use it like in this snippet.
Also note that the whole language dependency can be taken care of by the android framework.
Simply create different folders for each language. If english is your default language, just put the english strings into res/values/strings.xml
. Then create a new folder values-ru
and put the russian strings with identical names into res/values-ru/strings.xml
. From this point on android selects the correct one depending on the device locale for you, either when you call getString()
or when referencing strings in XML via @string/mystring
.
The ones from res/values/strings.xml
are the fallback ones, if you don't have a folder covering the users locale, this one will be used as default values.
See Localization and Providing Resources for more information.
Call string with a variable name
If I understand correctly, R.string.stringVariable
obviously will not work since R.string
is a compiled resource class and stringVariable
needs to be a dynamic value.
You can use these options instead
int resID = getResources().getIdentifier(stringVariable, "string", getPackageName());
textview.setText(resID); // directly set it
String content = getString(resID); // get the variable, then concatenate with other content
get string from Strings.xml by a variable
As far as I can tell, you could use public int getIdentifier (String name, String defType, String defPackage)
. Its use is discouraged, though.
To use it (I haven't done it but I had once read about the method) you probably would need to:
int identifier = getResources().getIdentifier ("title","string","your.package.name.here");
if (identifier!=0){
s=getResources().getString(identifier);
}
else{
s="";//or null or whatever
}
how to read value from string.xml in android?
Try this
String mess = getResources().getString(R.string.mess_1);
UPDATE
String string = getString(R.string.hello);
You can use either getString(int)
or getText(int)
to retrieve a string. getText(int)
will retain any rich text styling applied to the string.
Reference: https://developer.android.com/guide/topics/resources/string-resource.html
Related Topics
How to Store an Integer Array in Sharedpreferences
Override Android Backbutton Behavior Only Works on the First Page with Phonegap
Progressdialog Not Shown When Asynctask.Get() Called
How to Perform Collection Group Query Using Document Id in Cloud Firestore
Centering Recyclerview Items with Flexboxlayoutmanager
Answer Incoming Call Using Android.Telecom and Incallservice
Supporting Multiple Screen Size - Android
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3
How to Apply Zoom, Drag and Rotation to an Image in Android
Why Does My App Size on Device Differ Than the APK or Play Store Size
How to Send Data Back from Onpostexecute in an Asynctask
Org.JSON.JSONobject Cannot Be Converted to JSONarray in Android
Multiple Screen Support in Android
Can't Get Values Out of Ondatachange Method
How to Loop Through Dynamic JSON String Recursively in Android
Which Architecture Patterns Are Used on Android
Android: How to Make Keyboard Enter Button Say "Search" and Handle Its Click
Adding Opencv to Native C Code Through Cmake on Android Studio