Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i reference something in the /values/strings.xml file programatically?

i had some items in my strings.xml file that i want to change programatically, and originally i was doing it through a setText();call but now i am attempting to translate my app to a different language which means everything needs to be set in my strings.xml file. is it possible to put all the text for my app into a strings.xml and change things programatically through references to the string names, instead of using the setText() function call? for example how would i reference "GrandTotal"?

<string name="GrandTotal">Grand Total:</string>
<string name="choose_prompt">Choose a Mode</string>
like image 294
clayton33 Avatar asked Nov 27 '22 22:11

clayton33


2 Answers

You can use setText(R.string.GrandTotal);

If you don't have the possibility to set the text via resId directly you can use getString(R.string.GrandTotal);

like image 197
keyboardsurfer Avatar answered Dec 09 '22 20:12

keyboardsurfer


To avoid confusion between resourceIds and real ints, you could also use statements like

String s = getResources().getString( R.string.grand_total );

but for most ui methods an overload often provides support for passing directly resourceIds as @Keyboardsurfer said

like image 34
Snicolas Avatar answered Dec 09 '22 20:12

Snicolas