Skip to content

Most visited

Recently visited


Accessing Resources


  • Resources can be referenced from code using integers from, such as R.drawable.myimage
  • Resources can be referenced from resources using a special XML syntax, such as @drawable/myimage
  • You can also access your app resources with methods in Resources

Key classes

  1. Resources

In this document

  1. Accessing Resources from Code
  2. Accessing Resources from XML
    1. Referencing style attributes
  3. Accessing Platform Resources

See also

  1. Providing Resources
  2. Resource Types

Once you provide a resource in your application (discussed in Providing Resources), you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates.

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

Although the R class is where resource IDs are specified, you should never need to look there to discover a resource ID. A resource ID is always composed of:

There are two ways you can access a resource:

Accessing Resources in Code

You can use a resource in code by passing the resource ID as a method parameter. For example, you can set an ImageView to use the res/drawable/myimage.png resource using setImageResource():

ImageView imageView = (ImageView) findViewById(;

You can also retrieve individual resources using methods in Resources, which you can get an instance of with getResources().


Here's the syntax to reference a resource in code:


See Resource Types for more information about each resource type and how to reference them.

Use cases

There are many methods that accept a resource ID parameter and you can retrieve resources using methods in Resources. You can get an instance of Resources with Context.getResources().

Here are some examples of accessing resources in code:

// Load a background for the current screen from a drawable resource
getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

// Set the Activity title by getting a string from the Resources object, because
//  this method requires a CharSequence rather than a resource ID

// Load a custom layout for the current screen

// Set a slide in animation by getting an Animation from the Resources object

// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(;

Caution: You should never modify the file by hand—it is generated by the aapt tool when your project is compiled. Any changes are overridden next time you compile.

Accessing Resources from XML

You can define values for some XML attributes and elements using a reference to an existing resource. You will often do this when creating layout files, to supply strings and images for your widgets.

For example, if you add a Button to your layout, you should use a string resource for the button text:

    android:text="@string/submit" />


Here is the syntax to reference a resource in an XML resource:


See Resource Types for more information about each resource type and how to reference them.

Use cases

In some cases you must use a resource for a value in XML (for example, to apply a drawable image to a widget), but you can also use a resource in XML any place that accepts a simple value. For example, if you have the following resource file that includes a color resource and a string resource:

<?xml version="1.0" encoding="utf-8"?>
   <color name="opaque_red">#f00</color>
   <string name="hello">Hello!</string>

You can use these resources in the following layout file to set the text color and text string:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android=""
    android:text="@string/hello" />

In this case you don't need to specify the package name in the resource reference because the resources are from your own package. To reference a system resource, you would need to include the package name. For example:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android=""
    android:text="@string/hello" />

Note: You should use string resources at all times, so that your application can be localized for other languages. For information about creating alternative resources (such as localized strings), see Providing Alternative Resources. For a complete guide to localizing your application for other languages, see Localization.

You can even use resources in XML to create aliases. For example, you can create a drawable resource that is an alias for another drawable resource:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android=""
    android:src="@drawable/other_drawable" />

This sounds redundant, but can be very useful when using alternative resource. Read more about Creating alias resources.

Referencing style attributes

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:


For example, here's how you can reference an attribute to set the text color to match the "primary" text color of the system theme:

<EditText id="text"
    android:text="@string/hello_world" />

Here, the android:textColor attribute specifies the name of a style attribute in the current theme. Android now uses the value applied to the android:textColorSecondary style attribute as the value for android:textColor in this widget. Because the system resource tool knows that an attribute resource is expected in this context, you do not need to explicitly state the type (which would be ?android:attr/textColorSecondary)—you can exclude the attr type.

Accessing Platform Resources

Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name. For example, Android provides a layout resource you can use for list items in a ListAdapter:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));

In this example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of creating your own layout for list items. For more information, see the List View developer guide.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)