• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=String Resources
2parent.title=Resource Types
3parent.link=available-resources.html
4@jd:body
5
6<p>A string resource provides text strings for your application
7with optional text styling and formatting. There are three types of resources that can provide
8your application with strings:</p>
9
10<dl>
11  <dt><a href="#String">String</a></dt>
12    <dd>XML resource that provides a single string.</dd>
13  <dt><a href="#StringArray">String Array</a></dt>
14    <dd>XML resource that provides an array of strings.</dd>
15  <dt><a href="#Plurals">Quantity Strings (Plurals)</a></dt>
16    <dd>XML resource that carries different strings for different quantities
17    of the same word or phrase.</dd>
18</dl>
19
20<p>All strings are capable of applying some styling markup and formatting arguments. For
21information about styling and formatting strings, see the section about <a
22href="#FormattingAndStyling">Formatting and Styling</a>.</p>
23
24
25
26
27<h2 id="String">String</h2>
28
29<p>A single string that can be referenced from the application or from other resource files (such
30as an XML layout).</p>
31
32<p class="note"><strong>Note:</strong> A string is a simple resource that is referenced
33using the value provided in the {@code name} attribute (not the name of the XML file). So, you can
34combine string resources with other simple resources in the one XML file,
35under one {@code &lt;resources>} element.</p>
36
37<dl class="xml">
38
39<dt>file location:</dt>
40<dd><code>res/values/<em>filename</em>.xml</code><br/>
41The filename is arbitrary. The {@code &lt;string>} element's {@code name} will be used as the
42resource ID.</dd>
43
44<dt>compiled resource datatype:</dt>
45<dd>Resource pointer to a {@link java.lang.String}.</dd>
46
47<dt>resource reference:</dt>
48<dd>
49In Java: <code>R.string.<em>string_name</em></code><br/>
50In XML:<code>@string/<em>string_name</em></code>
51</dd>
52
53<dt>syntax:</dt>
54<dd>
55<pre class="stx">
56&lt;?xml version="1.0" encoding="utf-8"?>
57&lt;<a href="#string-resources-element">resources</a>>
58    &lt;<a href="#string-element">string</a>
59        name="<em>string_name</em>"
60        &gt;<em>text_string</em>&lt;/string&gt;
61&lt;/resources>
62</pre>
63</dd>
64
65<dt>elements:</dt>
66<dd>
67<dl class="tag-list">
68
69  <dt id="string-resources-element"><code>&lt;resources&gt;</code></dt>
70    <dd><strong>Required.</strong> This must be the root node.
71      <p>No attributes.</p>
72    </dd>
73  <dt id="string-element"><code>&lt;string&gt;</code></dt>
74    <dd>A string, which can include styling tags. Beware that you must escape apostrophes and
75quotation marks. For more information about how to properly style and format your strings see <a
76href="#FormattingAndStyling">Formatting and Styling</a>, below.
77      <p class="caps">attributes:</p>
78      <dl class="atn-list">
79        <dt><code>name</code></dt>
80        <dd><em>String</em>. A name for the string. This name will be used as the resource
81ID.</dd>
82      </dl>
83    </dd>
84
85</dl>
86</dd> <!-- end  elements and attributes -->
87
88<dt>example:</dt>
89<dd>XML file saved at <code>res/values/strings.xml</code>:
90<pre>
91&lt;?xml version="1.0" encoding="utf-8"?>
92&lt;resources>
93    &lt;string name="hello">Hello!&lt;/string>
94&lt;/resources>
95</pre>
96
97  <p>This layout XML applies a string to a View:</p>
98<pre>
99&lt;TextView
100    android:layout_width="fill_parent"
101    android:layout_height="wrap_content"
102    <strong>android:text="@string/hello"</strong> />
103</pre>
104
105  <p>This application code retrieves a string:</p>
106<pre>
107String string = {@link android.content.Context#getString(int) getString}(R.string.hello);
108</pre>
109<p>You can use either {@link android.content.Context#getString(int)} or
110{@link android.content.Context#getText(int)} to retrieve a string. {@link
111android.content.Context#getText(int)} will retain any rich text styling applied to the string.</p>
112
113</dd> <!-- end example -->
114
115</dl>
116
117
118
119
120
121
122
123
124
125<h2 id="StringArray">String Array</h2>
126
127<p>An array of strings that can be referenced from the application.</p>
128
129<p class="note"><strong>Note:</strong> A string array is a simple resource that is referenced
130using the value provided in the {@code name} attribute (not the name of the XML file). As
131such, you can combine string array resources with other simple resources in the one XML file,
132under one {@code &lt;resources>} element.</p>
133
134<dl class="xml">
135
136<dt>file location:</dt>
137<dd><code>res/values/<em>filename</em>.xml</code><br/>
138The filename is arbitrary. The {@code &lt;string-array>} element's {@code name} will be used as the
139resource ID.</dd>
140
141<dt>compiled resource datatype:</dt>
142<dd>Resource pointer to an array of {@link java.lang.String}s.</dd>
143
144<dt>resource reference:</dt>
145<dd>
146In Java: <code>R.array.<em>string_array_name</em></code>
147</dd>
148
149<dt>syntax:</dt>
150<dd>
151<pre class="stx">
152&lt;?xml version="1.0" encoding="utf-8"?>
153&lt;<a href="#string-array-resources-element">resources</a>>
154    &lt;<a href="#string-array-element">string-array</a>
155        name="<em>string_array_name</em>">
156        &lt;<a href="#string-array-item-element">item</a>
157            &gt;<em>text_string</em>&lt;/item&gt;
158    &lt;/string-array>
159&lt;/resources>
160</pre>
161</dd>
162
163<dt>elements:</dt>
164<dd>
165<dl class="tag-list">
166  <dt id="string-array-resources-element"><code>&lt;resources&gt;</code></dt>
167    <dd><strong>Required.</strong> This must be the root node.
168      <p>No attributes.</p>
169    </dd>
170  <dt id="string-array-element"><code>&lt;string-array&gt;</code></dt>
171    <dd>Defines an array of strings. Contains one or more {@code &lt;item>} elements.
172      <p class="caps">attributes:</p>
173      <dl class="atn-list">
174        <dt><code>name</code></dt>
175        <dd><em>String</em>. A name for the array. This name will be used as the resource
176ID to reference the array.</dd>
177      </dl>
178
179    </dd>
180  <dt id="string-array-item-element"><code>&lt;item&gt;</code></dt>
181    <dd>A string, which can include styling tags. The value can be a referenced to another
182string resource. Must be a child of a {@code &lt;string-array&gt;} element. Beware that you
183must escape apostrophes and
184quotation marks. See <a href="#FormattingAndStyling">Formatting and Styling</a>, below, for
185information about to properly style and format your strings.
186      <p>No attributes.</p>
187    </dd>
188</dl>
189</dd> <!-- end  elements -->
190
191<dt>example:</dt>
192<dd>XML file saved at <code>res/values/strings.xml</code>:
193<pre>
194&lt;?xml version="1.0" encoding="utf-8"?>
195&lt;resources>
196    &lt;string-array name="planets_array">
197        &lt;item>Mercury&lt;/item>
198        &lt;item>Venus&lt;/item>
199        &lt;item>Earth&lt;/item>
200        &lt;item>Mars&lt;/item>
201    &lt;/string-array>
202&lt;/resources>
203</pre>
204
205  <p>This application code retrieves a string array:</p>
206<pre>
207Resources res = {@link android.content.Context#getResources()};
208String[] planets = res.{@link android.content.res.Resources#getStringArray(int)
209getStringArray}(R.array.planets_array);
210</pre>
211</dd> <!-- end example -->
212
213</dl>
214
215
216
217
218
219
220
221<h2 id="Plurals">Quantity Strings (Plurals)</h2>
222
223<p>Different languages have different rules for grammatical agreement with quantity. In English,
224for example, the quantity 1 is a special case. We write "1 book", but for any other quantity we'd
225write "<i>n</i> books". This distinction between singular and plural is very common, but other
226languages make finer distinctions. The full set supported by Android is <code>zero</code>,
227<code>one</code>, <code>two</code>, <code>few</code>, <code>many</code>, and <code>other</code>.
228
229<p>The rules for deciding which case to use for a given language and quantity can be very complex,
230so Android provides you with methods such as
231{@link android.content.res.Resources#getQuantityString(int,int) getQuantityString()} to select
232the appropriate resource for you.
233
234<p>Note that the selection is made based on grammatical necessity. A string for <code>zero</code>
235in English will be ignored even if the quantity is 0, because 0 isn't grammatically different
236from 2, or any other number except 1 ("zero books", "one book", "two books", and so on).
237Don't be misled either by the fact that, say, <code>two</code> sounds like it could only apply to
238the quantity 2: a language may require that 2, 12, 102 (and so on) are all treated like one
239another but differently to other quantities. Rely on your translator to know what distinctions
240their language actually insists upon.
241
242<p>It's often possible to avoid quantity strings by using quantity-neutral formulations such as
243"Books: 1". This will make your life and your translators' lives easier, if it's a style that's
244in keeping with your application.
245
246<p class="note"><strong>Note:</strong> A plurals collection is a simple resource that is
247referenced using the value provided in the {@code name} attribute (not the name of the XML
248file). As such, you can combine plurals resources with other simple resources in the one
249XML file, under one {@code &lt;resources>} element.</p>
250
251<dl class="xml">
252
253<dt>file location:</dt>
254<dd><code>res/values/<em>filename</em>.xml</code><br/>
255The filename is arbitrary. The {@code &lt;plurals>} element's {@code name} will be used as the
256resource ID.</dd>
257
258<dt>resource reference:</dt>
259<dd>
260In Java: <code>R.plurals.<em>plural_name</em></code>
261</dd>
262
263<dt>syntax:</dt>
264<dd>
265<pre class="stx">
266&lt;?xml version="1.0" encoding="utf-8"?>
267&lt;<a href="#plurals-resources-element">resources</a>>
268    &lt;<a href="#plurals-element">plurals</a>
269        name="<em>plural_name</em>">
270        &lt;<a href="#plurals-item-element">item</a>
271            quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
272            &gt;<em>text_string</em>&lt;/item>
273    &lt;/plurals>
274&lt;/resources>
275</pre>
276</dd>
277
278<dt>elements:</dt>
279<dd>
280<dl class="tag-list">
281
282  <dt id="plurals-resources-element"><code>&lt;resources&gt;</code></dt>
283    <dd><strong>Required.</strong> This must be the root node.
284      <p>No attributes.</p>
285    </dd>
286  <dt id="plurals-element"><code>&lt;plurals&gt;</code></dt>
287    <dd>A collection of strings, of which, one string is provided depending on the amount of
288something. Contains one or more {@code &lt;item>} elements.
289      <p class="caps">attributes:</p>
290      <dl class="atn-list">
291        <dt><code>name</code></dt>
292        <dd><em>String</em>. A name for the pair of strings. This name will be used as the
293resource ID.</dd>
294      </dl>
295
296    </dd>
297  <dt id="plurals-item-element"><code>&lt;item&gt;</code></dt>
298    <dd>A plural or singular string. The value can be a referenced to another
299string resource. Must be a child of a {@code &lt;plurals&gt;} element. Beware that you must
300escape apostrophes and quotation marks. See <a href="#FormattingAndStyling">Formatting and
301Styling</a>, below, for information about to properly style and format your strings.
302      <p class="caps">attributes:</p>
303      <dl class="atn-list">
304        <dt><code>quantity</code></dt>
305        <dd><em>Keyword</em>. A value indicating when this string should be used. Valid
306values, with non-exhaustive examples in parentheses:
307          <table>
308            <tr><th>Value</th><th>Description</th></tr>
309            <tr>
310              <td>{@code zero}</td><td>When the language requires special treatment of the number 0 (as in Arabic).</td>
311            </tr>
312            <tr>
313              <td>{@code one}</td><td>When the language requires special treatment of numbers like one (as with the number 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 is in this class).</td>
314            </tr>
315            <tr>
316              <td>{@code two}</td><td>When the language requires special treatment of numbers like two (as in Welsh).</td>
317            </tr>
318            <tr>
319              <td>{@code few}</td><td>When the language requires special treatment of "small" numbers (as with 2, 3, and 4 in Czech; or numbers ending 2, 3, or 4 but not 12, 13, or 14 in Polish).</td>
320            </tr>
321            <tr>
322              <td>{@code many}</td><td>When the language requires special treatment of "large" numbers (as with numbers ending 11-99 in Maltese).</td>
323            </tr>
324            <tr>
325              <td>{@code other}</td><td>When the language does not require special treatment of the given quantity.</td>
326            </tr>
327          </table>
328        </dd>
329      </dl>
330    </dd>
331
332</dl>
333</dd> <!-- end elements -->
334
335<dt>example:</dt>
336<dd>XML file saved at {@code res/values/strings.xml}:</p>
337<pre>
338&lt;?xml version="1.0" encoding="utf-8"?>
339&lt;resources>
340    &lt;plurals name="numberOfSongsAvailable">
341        &lt;item quantity="one">One song found.&lt;/item>
342        &lt;item quantity="other">%d songs found.&lt;/item>
343    &lt;/plurals>
344&lt;/resources>
345</pre>
346    <p>XML file saved at {@code res/values-pl/strings.xml}:</p>
347<pre>
348&lt;?xml version="1.0" encoding="utf-8"?>
349&lt;resources>
350    &lt;plurals name="numberOfSongsAvailable">
351        &lt;item quantity="one">Znaleziono jedn&#x0105; piosenk&#x0119;.&lt;/item>
352        &lt;item quantity="few">Znaleziono %d piosenki.&lt;/item>
353        &lt;item quantity="other">Znaleziono %d piosenek.&lt;/item>
354    &lt;/plurals>
355&lt;/resources>
356</pre>
357    <p>Java code:</p>
358<pre>
359int count = getNumberOfsongsAvailable();
360Resources res = {@link android.content.Context#getResources()};
361String songsFound = res.{@link android.content.res.Resources#getQuantityString(int,int)
362getQuantityString}(R.plurals.numberOfSongsAvailable, count, count);
363</pre>
364<p>When using the {@link android.content.res.Resources#getQuantityString(int,int)
365getQuantityString()} method, you need to pass the {@code count} twice if your string includes
366<a href="#FormattingAndStyling">string formatting</a> with a number. For example, for the string
367{@code %d songs found}, the first {@code count} parameter selects the appropriate plural string and
368the second {@code count} parameter is inserted into the {@code %d} placeholder. If your plural
369strings do not include string formatting, you don't need to pass the third parameter to {@link
370android.content.res.Resources#getQuantityString(int,int) getQuantityString}.</p>
371</dd> <!-- end example -->
372
373</dl>
374
375
376
377
378
379
380
381
382<h2 id="FormattingAndStyling">Formatting and Styling</h2>
383
384<p>Here are a few important things you should know about how to properly
385format and style your string resources.</p>
386
387
388<h3>Escaping apostrophes and quotes</h3>
389
390<p>If you have an apostrophe or a quote in your string, you must either escape it or enclose the
391whole string in the other type of enclosing quotes. For example, here are some stings that
392do and don't work:</p>
393
394<pre>
395&lt;string name="good_example">"This'll work"&lt;/string>
396&lt;string name="good_example_2">This\'ll also work&lt;/string>
397&lt;string name="bad_example">This doesn't work&lt;/string>
398&lt;string name="bad_example_2">XML encodings don&amp;apos;t work&lt;/string>
399</pre>
400
401
402<h3>Formatting strings</h3>
403
404<p>If you need to format your strings using <a
405href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
406java.lang.Object...)">{@code String.format(String, Object...)}</a>,
407then you can do so by putting
408your format arguments in the string resource. For example, with the following resource:</p>
409
410<pre>
411&lt;string name="welcome_messages">Hello, %1$s! You have %2$d new messages.&lt;/string>
412</pre>
413
414<p>In this example, the format string has two arguments: {@code %1$s} is a string and {@code %2$d}
415is a decimal number. You can format the string with arguments from your application like this:</p>
416
417<pre>
418Resources res = {@link android.content.Context#getResources()};
419String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
420java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount);
421</pre>
422
423
424
425<h3>Styling with HTML markup</h3>
426
427<p>You can add styling to your strings with HTML markup. For example:</p>
428<pre>
429&lt;?xml version="1.0" encoding="utf-8"?>
430&lt;resources>
431    &lt;string name="welcome">Welcome to &lt;b>Android&lt;/b>!&lt;/string>
432&lt;/resources>
433</pre>
434<p>Supported HTML elements include:</p>
435<ul>
436  <li>{@code &lt;b>} for <b>bold</b> text.</li>
437  <li>{@code &lt;i>} for <i>italic</i> text.</li>
438  <li>{@code &lt;u>} for <u>underline</u> text.</li>
439</ul>
440
441<p>Sometimes you may want to create a styled text resource that is also used as a format
442string. Normally, this won't work because the <a
443href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
444java.lang.Object...)">{@code String.format(String, Object...)}</a>
445method will strip all the style
446information from the string. The work-around to this is to write the HTML tags with escaped
447entities, which are then recovered with {@link android.text.Html#fromHtml(String)},
448after the formatting takes place. For example:</p>
449
450<ol>
451  <li>Store your styled text resource as an HTML-escaped string:
452<pre>
453&lt;resources&gt;
454  &lt;string name="welcome_messages"&gt;Hello, %1$s! You have &amp;lt;b>%2$d new messages&amp;lt;/b>.&lt;/string>
455&lt;/resources&gt;
456</pre>
457<p>In this formatted string, a {@code &lt;b>} element is added. Notice that the opening bracket is
458HTML-escaped, using the {@code &amp;lt;} notation.</p>
459  </li>
460  <li>Then format the string as usual, but also call {@link android.text.Html#fromHtml} to
461convert the HTML text into styled text:
462<pre>
463Resources res = {@link android.content.Context#getResources()};
464String text = String.<a
465href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
466java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), username, mailCount);
467CharSequence styledText = Html.fromHtml(text);
468</pre>
469  </li>
470</ol>
471
472<p>Because the {@link android.text.Html#fromHtml} method will format all HTML entities, be sure to
473escape any possible HTML characters in the strings you use with the formatted text, using
474{@link android.text.TextUtils#htmlEncode}. For instance, if you'll be passing a string argument to
475<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
476java.lang.Object...)">{@code String.format()}</a> that may contain characters such as
477"&lt;" or "&amp;", then they must be escaped before formatting, so that when the formatted string
478is passed through {@link android.text.Html#fromHtml}, the characters come out the way they were
479originally written. For example:</p>
480<pre>
481String escapedUsername = TextUtil.{@link android.text.TextUtils#htmlEncode htmlEncode}(username);
482
483Resources res = {@link android.content.Context#getResources()};
484String text = String.<a href="{@docRoot}reference/java/lang/String.html#format(java.lang.String,
485java.lang.Object...)">format</a>(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
486CharSequence styledText = Html.fromHtml(text);
487</pre>
488
489
490
491