1page.title=Accessing Resources
2parent.title=Application Resources
3parent.link=index.html
4@jd:body
5
6<div id="qv-wrapper">
7<div id="qv">
8  <h2>Quickview</h2>
9  <ul>
10    <li>Resources can be referenced from code using integers from {@code R.java}, such as
11{@code R.drawable.myimage}</li>
12    <li>Resources can be referenced from resources using a special XML syntax, such as {@code
13&#64;drawable/myimage}</li>
14    <li>You can also access your app resources with methods in
15{@link android.content.res.Resources}</li>
16  </ul>
17
18  <h2>Key classes</h2>
19  <ol>
20    <li>{@link android.content.res.Resources}</li>
21  </ol>
22
23  <h2>In this document</h2>
24  <ol>
25    <li><a href="#ResourcesFromCode">Accessing Resources from Code</a></li>
26    <li><a href="#ResourcesFromXml">Accessing Resources from XML</a>
27      <ol>
28        <li><a href="#ReferencesToThemeAttributes">Referencing style attributes</a></li>
29      </ol>
30    </li>
31    <li><a href="#PlatformResources">Accessing Platform Resources</a></li>
32  </ol>
33
34  <h2>See also</h2>
35  <ol>
36    <li><a href="providing-resources.html">Providing Resources</a></li>
37    <li><a href="available-resources.html">Resource Types</a></li>
38  </ol>
39</div>
40</div>
41
42
43
44
45<p>Once you provide a resource in your application (discussed in <a
46href="providing-resources.html">Providing Resources</a>), you can apply it by
47referencing its resource ID. All resource IDs are defined in your project's {@code R} class, which
48the {@code aapt} tool automatically generates.</p>
49
50<p>When your application is compiled, {@code aapt} generates the {@code R} class, which contains
51resource IDs for all the resources in your {@code
52res/} directory. For each type of resource, there is an {@code R} subclass (for example,
53{@code R.drawable} for all drawable resources) and for each resource of that type, there is a static
54integer (for example, {@code R.drawable.icon}). This integer is the resource ID that you can use
55to retrieve your resource.</p>
56
57<p>Although the {@code R} class is where resource IDs are specified, you should never need to
58look there to discover a resource ID. A resource ID is always composed of:</p>
59<ul>
60  <li>The <em>resource type</em>: Each resource is grouped into a "type," such as {@code
61string}, {@code drawable}, and {@code layout}. For more about the different types, see <a
62href="available-resources.html">Resource Types</a>.
63  </li>
64  <li>The <em>resource name</em>, which is either: the filename,
65excluding the extension; or the value in the XML {@code android:name} attribute, if the
66resource is a simple value (such as a string).</li>
67</ul>
68
69<p>There are two ways you can access a resource:</p>
70<ul>
71  <li><strong>In code:</strong> Using an static integer from a sub-class of your {@code R}
72class, such as:
73    <pre class="classic no-pretty-print">R.string.hello</pre>
74    <p>{@code string} is the resource type and {@code hello} is the resource name. There are many
75Android APIs that can access your resources when you provide a resource ID in this format. See
76<a href="#ResourcesFromCode">Accessing Resources in Code</a>.</p>
77  </li>
78  <li><strong>In XML:</strong> Using a special XML syntax that also corresponds to
79the resource ID defined in your {@code R} class, such as:
80    <pre class="classic no-pretty-print">&#64;string/hello</pre>
81    <p>{@code string} is the resource type and {@code hello} is the resource name. You can use this
82syntax in an XML resource any place where a value is expected that you provide in a resource. See <a
83href="#ResourcesFromXml">Accessing Resources from XML</a>.</p>
84  </li>
85</ul>
86
87
88
89<h2 id="ResourcesFromCode">Accessing Resources in Code </h2>
90
91<p>You can use a resource in code by passing the resource ID as a method parameter. For
92example, you can set an {@link android.widget.ImageView} to use the {@code res/drawable/myimage.png}
93resource using {@link android.widget.ImageView#setImageResource(int) setImageResource()}:</p>
94<pre>
95ImageView imageView = (ImageView) findViewById(R.id.myimageview);
96imageView.setImageResource(<strong>R.drawable.myimage</strong>);
97</pre>
98
99<p>You can also retrieve individual resources using methods in {@link
100android.content.res.Resources}, which you can get an instance of
101with {@link android.content.Context#getResources()}.</p>
102
103<div class="sidebox-wrapper">
104<div class="sidebox">
105<h2>Access to Original Files</h2>
106
107<p>While uncommon, you might need access your original files and directories. If you do, then
108saving your files in {@code res/} won't work for you, because the only way to read a resource from
109{@code res/} is with the resource ID. Instead, you can save your resources in the
110{@code assets/} directory.</p>
111<p>Files saved in the {@code assets/} directory are <em>not</em> given a resource
112ID, so you can't reference them through the {@code R} class or from XML resources. Instead, you can
113query files in the {@code assets/} directory like a normal file system and read raw data using
114{@link android.content.res.AssetManager}.</p>
115<p>However, if all you require is the ability to read raw data (such as a video or audio file),
116then save the file in the {@code res/raw/} directory and read a stream of bytes using {@link
117android.content.res.Resources#openRawResource(int) openRawResource()}.</p>
118
119</div>
120</div>
121
122
123<h3>Syntax</h3>
124
125<p>Here's the syntax to reference a resource in code:</p>
126
127<pre class="classic no-pretty-print">
128[<em>&lt;package_name&gt;</em>.]R.<em>&lt;resource_type&gt;</em>.<em>&lt;resource_name&gt;</em>
129</pre>
130
131<ul>
132  <li><em>{@code &lt;package_name&gt;}</em> is the name of the package in which the resource is located (not
133required when referencing resources from your own package).</li>
134  <li><em>{@code &lt;resource_type&gt;}</em> is the {@code R} subclass for the resource type.</li>
135  <li><em>{@code &lt;resource_name&gt;}</em> is either the resource filename
136without the extension or the {@code android:name} attribute value in the XML element (for simple
137values).</li>
138</ul>
139<p>See <a href="available-resources.html">Resource Types</a> for
140more information about each resource type and how to reference them.</p>
141
142
143<h3>Use cases</h3>
144
145<p>There are many methods that accept a resource ID parameter and you can retrieve resources using
146methods in {@link android.content.res.Resources}. You can get an instance of  {@link
147android.content.res.Resources} with {@link android.content.Context#getResources
148Context.getResources()}.</p>
149
150
151<p>Here are some examples of accessing resources in code:</p>
152
153<pre>
154// Load a background for the current screen from a drawable resource
155{@link android.app.Activity#getWindow()}.{@link
156android.view.Window#setBackgroundDrawableResource(int)
157setBackgroundDrawableResource}(<strong>R.drawable.my_background_image</strong>) ;
158
159// Set the Activity title by getting a string from the Resources object, because
160//  this method requires a CharSequence rather than a resource ID
161{@link android.app.Activity#getWindow()}.{@link android.view.Window#setTitle(CharSequence)
162setTitle}(getResources().{@link android.content.res.Resources#getText(int)
163getText}(<strong>R.string.main_title</strong>));
164
165// Load a custom layout for the current screen
166{@link android.app.Activity#setContentView(int)
167setContentView}(<strong>R.layout.main_screen</strong>);
168
169// Set a slide in animation by getting an Animation from the Resources object
170mFlipper.{@link android.widget.ViewAnimator#setInAnimation(Animation)
171setInAnimation}(AnimationUtils.loadAnimation(this,
172        <strong>R.anim.hyperspace_in</strong>));
173
174// Set the text on a TextView object using a resource ID
175TextView msgTextView = (TextView) findViewById(<strong>R.id.msg</strong>);
176msgTextView.{@link android.widget.TextView#setText(int)
177setText}(<strong>R.string.hello_message</strong>);
178</pre>
179
180
181<p class="caution"><strong>Caution:</strong> You should never modify the {@code
182R.java} file by hand&mdash;it is generated by the {@code aapt} tool when your project is
183compiled. Any changes are overridden next time you compile.</p>
184
185
186
187<h2 id="ResourcesFromXml">Accessing Resources from XML</h2>
188
189<p>You can define values for some XML attributes and elements using a
190reference to an existing resource. You will often do this when creating layout files, to
191supply strings and images for your widgets.</p>
192
193<p>For example, if you add a {@link android.widget.Button} to your layout, you should use
194a <a href="string-resource.html">string resource</a> for the button text:</p>
195
196<pre>
197&lt;Button
198    android:layout_width="fill_parent"
199    android:layout_height="wrap_content"
200    android:text="<strong>@string/submit</strong>" /&gt;
201</pre>
202
203
204<h3>Syntax</h3>
205
206<p>Here is the syntax to reference a resource in an XML resource:</p>
207
208<pre class="classic no-pretty-print">
209&#64;[<em>&lt;package_name&gt;</em>:]<em>&lt;resource_type&gt;</em>/<em>&lt;resource_name&gt;</em>
210</pre>
211
212<ul>
213  <li>{@code &lt;package_name&gt;} is the name of the package in which the resource is located (not
214required when referencing resources from the same package)</li>
215  <li>{@code &lt;resource_type&gt;} is the
216{@code R} subclass for the resource type</li>
217  <li>{@code &lt;resource_name&gt;} is either the resource filename
218without the extension or the {@code android:name} attribute value in the XML element (for simple
219values).</li>
220</ul>
221
222<p>See <a href="available-resources.html">Resource Types</a> for
223more information about each resource type and how to reference them.</p>
224
225
226<h3>Use cases</h3>
227
228<p>In some cases you must use a resource for a value in XML (for example, to apply a drawable image
229to a widget), but you can also use a resource in XML any place that accepts a simple value. For
230example, if you have the following resource file that includes a <a
231href="more-resources.html#Color">color resource</a> and a <a
232href="string-resource.html">string resource</a>:</p>
233
234<pre>
235&lt;?xml version="1.0" encoding="utf-8"?>
236&lt;resources>
237   &lt;color name="opaque_red">#f00&lt;/color>
238   &lt;string name="hello">Hello!&lt;/string>
239&lt;/resources>
240</pre>
241
242<p>You can use these resources in the following layout file to set the text color and
243text string:</p>
244
245<pre>
246&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
247&lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android";
248    android:layout_width=&quot;fill_parent&quot;
249    android:layout_height=&quot;fill_parent&quot;
250    android:textColor=&quot;<strong>&#64;color/opaque_red</strong>&quot;
251    android:text=&quot;<strong>&#64;string/hello</strong>&quot; /&gt;
252</pre>
253
254<p>In this case you don't need to specify the package name in the resource reference because the
255resources are from your own package. To
256reference a system resource, you would need to include the package name. For example:</p>
257
258<pre>
259&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
260&lt;EditText xmlns:android=&quot;http://schemas.android.com/apk/res/android";
261    android:layout_width=&quot;fill_parent&quot;
262    android:layout_height=&quot;fill_parent&quot;
263    android:textColor=&quot;<strong>&#64;android:color/secondary_text_dark</strong>&quot;
264    android:text=&quot;&#64;string/hello&quot; /&gt;
265</pre>
266
267<p class="note"><strong>Note:</strong> You should use string resources at all times, so that your
268application can be localized for other languages. For information about creating alternative
269resources (such as localized strings), see <a
270href="providing-resources.html#AlternativeResources">Providing Alternative
271Resources</a>.</p>
272
273<p>You can even use resources in XML to create aliases. For example, you can create a
274drawable resource that is an alias for another drawable resource:</p>
275
276<pre>
277&lt;?xml version="1.0" encoding="utf-8"?>
278&lt;bitmap xmlns:android="http://schemas.android.com/apk/res/android"
279    android:src="@drawable/other_drawable" />
280</pre>
281
282<p>This sounds redundant, but can be very useful when using alternative resource. Read more about
283<a href="providing-resources.html#AliasResources">Creating alias resources</a>.</p>
284
285
286
287<h3 id="ReferencesToThemeAttributes">Referencing style attributes</h3>
288
289<p>A style attribute resource allows you to reference the value
290of an attribute in the currently-applied theme. Referencing a style attribute allows you to
291customize the look of UI elements by styling them to match standard variations supplied by the
292current theme, instead of supplying a hard-coded value. Referencing a style attribute
293essentially says, "use the style that is defined by this attribute, in the current theme."</p>
294
295<p>To reference a style attribute, the name syntax is almost identical to the normal resource
296format, but instead of the at-symbol ({@code &#64;}), use a question-mark ({@code ?}), and the
297resource type portion is optional. For instance:</p>
298
299<pre class="classic">
300?[<em>&lt;package_name&gt;</em>:][<em>&lt;resource_type&gt;</em>/]<em>&lt;resource_name&gt;</em>
301</pre>
302
303<p>For example, here's how you can reference an attribute to set the text color to match the
304"primary" text color of the system theme:</p>
305
306<pre>
307&lt;EditText id=&quot;text&quot;
308    android:layout_width=&quot;fill_parent&quot;
309    android:layout_height=&quot;wrap_content&quot;
310    android:textColor=&quot;<strong>?android:textColorSecondary</strong>&quot;
311    android:text=&quot;&#64;string/hello_world&quot; /&gt;
312</pre>
313
314<p>Here, the {@code android:textColor} attribute specifies the name of a style attribute
315in the current theme. Android now uses the value applied to the {@code android:textColorSecondary}
316style attribute as the value for {@code android:textColor} in this widget. Because the system
317resource tool knows that an attribute resource is expected in this context,
318you do not need to explicitly state the type (which would be
319<code>?android:attr/textColorSecondary</code>)&mdash;you can exclude the {@code attr} type.</p>
320
321
322
323
324<h2 id="PlatformResources">Accessing Platform Resources</h2>
325
326<p>Android contains a number of standard resources, such as styles, themes, and layouts. To
327access these resource, qualify your resource reference with the
328<code>android</code> package name. For example, Android provides a layout resource you can use for
329list items in a {@link android.widget.ListAdapter}:</p>
330
331<pre>
332{@link android.app.ListActivity#setListAdapter(ListAdapter)
333setListAdapter}(new {@link
334android.widget.ArrayAdapter}&lt;String&gt;(this, <strong>android.R.layout.simple_list_item_1</strong>, myarray));
335</pre>
336
337<p>In this example, {@link android.R.layout#simple_list_item_1} is a layout resource defined by the
338platform for items in a {@link android.widget.ListView}. You can use this instead of creating
339your own layout for list items. For more information, see the
340<a href="{@docRoot}guide/topics/ui/layout/listview.html">List View</a> developer guide.</p>
341
342