Friday, July 15, 2011

Using String Resources

For quick string references, like a global string, you should try and use the string resource. You can set a string resource by opening the file in your project explorer located at values/strings.xml. In this file is where we will define our strings.

To manually enter a string (assuming your in Eclipse) double click the values/strings.xml file, when the new window opens at the bottom select "strings.xml". In here you can add your own strings in the following format: <string name="{a_name_here}">{you put the string's value in here}</string>

for example, I could store a URL for later use, it would look something like..

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_url_1">http://google.com/</string>
</resource>

We can reference that string in our code like this:

String s = getString(R.string.app_url_1);