4

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>

4 Answers 4

20

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);

Sign up to request clarification or add additional context in comments.

1 Comment

how do you do the same from within a static method, I can't access getString in a static method. :-\
6

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

1 Comment

thanks, both of the items that you and keyboardsurfer provided helped to be the fix for me!
1

Try this way . I hope it helps you .

setText(getResources().getString(R.string.GrandTotal));

Comments

0

in an Acrivity:

String str = getString(R.string.choose_prompt);

or

String str = this.getString(R.string.choose_prompt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.