You should place each type of resources in a specific subdirectory of your project’s res/ directory. For example, here’s the file hierarchy for a simple project:

MyProject/
    src/  
        MyActivity.java  
    res/
        drawable/  
            graphic.png  
        layout/  
            main.xml
            info.xml
        mipmap/  
            icon.png 
        values/  
            strings.xml

As you can see in this example, the res/ directory contains all the resources (in subdirectories): an image resource, two layout resources, mipmap/ directories for launcher icons, and a string resource file. The resource directory names are important and are described below.

DirectoryResource Type
animator/XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class. See Animation Resources
anim/XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)
color/XML files that define a state list of colors.
See Google Material Colors.
More detail about Color State List Resource.
drawable/

Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:

• Bitmap files
• Nine-Patches (re-sizable bitmaps)
• State lists
• Shapes
• Animation drawables
• Other drawables

See Drawable Resources.

mipmap/Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders.
layout/XML files that define a user interface layout. See Layout Resource
menu/XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource
raw/

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/are not given a resource ID.

values/

XML files that contain simple values, such as strings, integers, and colors.

Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.

Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:

arrays.xml for resource arrays, and accessed from the R.array class.
bools.xml for resource boolean, and accessed from the R.bool class.
integers.xml for resource integers, and accessed from the R.integer class.
colors.xml for color values, and accessed from the R.color class.
dimens.xml for dimension values, and accessed from the R.dimen class.
strings.xml for string values, and accessed from the R.string class. See String Resources.
styles.xml for styles, and accessed from the R.style class. See Style Resource.

xml/Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

Accessing Resources in Java 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/image.png resource using setImageResource():

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.image);

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 TextView to your layout, you should use a string resource for the Username text:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/red" />
    android:text="@string/username" />

Value Resource File

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="red">#f00</color>
   <string name="username">Username</string>
</resources>
Subscribe To Android Learning

Subscribe To Android Learning

Join our mailing list to receive the latest android tutorials from our site.

You have Successfully Subscribed!

Pin It on Pinterest

Shares
Share This