Android - String

> Android

1 - About

String is a resource

Advertising

3 - Location

Located in /res/values/strings.xml

4 - Formatting

Bad practice:

textView.setText(variable + " unitSymbol");

Good practice:

<!-- import the xliff namespace in the strings.xml -->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Format it -->
<string name="welcome">
         Hi <xliff:g id="name">%1$s</xliff:g> 
         You have <xliff:g id="count">%2$d</xliff:g> new messages
</string>
  • Context.getString. From the context, you can get access to the string XML declaration.
String s = context.getString(R.string.welcome,myNameVariable, myCountVariable);

5 - Localization

6 - Example

<resources>
    <string name="app_name">Sunshine</string>
    <string name="action_settings">Settings</string>
    <string name="refresh">Refresh</string>
</resources>

7 - Documentation / Reference

Advertising