Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Using String Resources

how to add a string in strings.xml

usage of strings.xml?

2 Answers

Valerie Makumire
Valerie Makumire
3,262 Points

Go into strings.xml which is located in the values folder. Then use the following format to add a string to it.

<string name="NameOfTheString">Some string that you want</string>

Then you can use the string in other XML files by referencing it like this:

@string/NameOfTheString

To use the string in your Java files you must use the getString() method like this:

getString(R.string.NameOfTheString);

Be aware though you cannot use this in classes outside activities unless you manage to get a context variable.

thank u so much, and how to set the text of a button using a string??can u help me