KIO
Kreative Ideen online
String Resource Files

String Resource Files

string.xml is the default resource file used to hold name/value pairs of Strings sot that they can be referenced throughout your app. It has the following format:

[php] <resources> <string name="app_name">Beer Adviser</string> <string name="find_beer">Find Beer!</string> <string name="brands">No beer selected</string> </resources> [/php]

There are two things that allwo Android to recognize strings.xml as being a String resource file:

  • The file is held in the folder app/src/main/res/values.
    XML files held in this folder contain simple values, such as Strings and colors.
  • The file has a <resources> element, wich contains one or more <string> elements.
    The format of the file itself indicates that it’s a resource file containing Strings. The <resources> element tells Android that the file contains resources, and the <string> element identifies each string resource. This means that you don’t need to call your String resource file strings.xml; if you want, your can call it something else, or split your Strings into multiple files.
    Each name/value pair takes the form
    <string name=“string_name”>string_value</string>
    where string_name is the identifier of the String, and string_value is the String value itself.
    A layout can retrieve the value of the String using:
    “@string/string_name”
  • To add an arry of Strings(for a spinner for example), use the following syntax
    <string-array name=”beer_colors“>
    <item>light</item>
    <item>amber</item>
    <item>dark</item>
    </string-array>

    To attach the values to a spinner use the following syntax
    android:entries=”@array/beer_colors” />

To create different String files for different languages

Put your default English Strings resource file in the app/src/main/res/value folder as normal, and your French resource file in a new folder called app/src/main/res/values-fr. If the device is set to French, it will use the String in the app/src/main/res/values-fr folder. If the device is set to any other language, it will use the Strings in app/src/main/res/values.

Leave a Reply

Your email address will not be published. Required fields are marked *