How to programmatically reference <string> in xml file in android
Solution 1
Use setText(getResources().getString(R.string.one));
Solution 2
Get the resources of the application, and then get a string with the ID you are looking for.
getResources().getString(R.string.one);
Solution 3
To determine which string you use with a variable you will have to use a switch, as below
switch(anyInt) {
case 1://if the int == 1, then the textview will be set to this
tv.setText(getResources().getString(R.string.one);
break;
case 2://if the into == 2 then the TV will be set to this
tv.setText(getResources().getString(R.string.two
break;
default:
tv.setText("into does not have value 1-2")
}
Add as many of these statements as you need, I believe it is very memory efficient even if you have a lot of statements
Solution 4
I Think you need this if i am wrong then get back to me.
Try this.
getResources().getString(R.string.app_name);
You just simply read the resource of your application .You can use any resource of your application by getResources()
of Resource Class.
Now , Here you need to read string form String.xml
so you can use getString()
which is the method of resource so you will get your output Now.
Comments
-
thunderson almost 2 years
I am new to android programming and wanna know this:
I have 3 strings defined in the strings.xml file:
<resources> <string name="one">First Click </string> <string name="two">Second Click </string> <string name="three">Third Click </string> <resources>
and a text view which displays the first string in the strings.xml file. i don't want to use
settext("******")
to change the text of text view when the user clicks on a button. how can i make the textview switch to the text already defined in the strings.xml file, say fromFirst Click to Second Click
-
Niranj Patel over 11 yearsi don't want to use settext("******") did you read this in question?
-
James McCracken over 11 yearswhy exactly don't you want to use settext()? there is no other way.
-
thunderson over 11 years.thank you very much....but pls in a case where there are a lot of strings specified in the strings.xml file, is there a way a variable can be used to to determine the string to show....for instance setText(getResources().getString(R.string.VARIABLE)); or something like that...so that it can show different texts..
-
thunderson over 11 yearspls...can the id be a variable....if not...how do i use a variable to determine which string to print out?
-
thunderson over 11 yearsi want to use a variable to determine the string to print out....i.e. a variable that will determine whether the first string or the last string, etc. in the strings.xml file should be printed out
-
thunderson over 11 yearsthank you very much....but pls in a case where there are a lot of strings specified in the strings.xml file, is there a way a variable can be used to to determine the string to show....for instance setText(getResources().getString(R.string.VARIABLE)); or something like that...so that it can show different texts...depending on certain user actions...
-
Chintan Khetiya over 11 yearscan you give example which type of user input can change string .? mean for what type of input that can be change my output string .
-
Matt Clark over 11 yearsThe ID is just an integer that references a point in memory specified by the auto-compiled R file. You could use an integer variable, but chances are the point in memory will change, and your strings will get all mixed up. For example, R.string.one may be 0x7f06000b one run, and 0x7c0af0cb the next. You could always use a switch(var){case:} statement.
-
Android Boy over 11 yearsI can't understand you, what do you want. Please post a small example here. If you satisfy for my answer, then please check the tick mark on my answer.