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.
|XML files that define property animations. They are saved in |
|XML files that define tween animations. (Property animations can also be saved in this directory, but the |
|XML files that define a state list of colors.|
See Google Material Colors.
More detail about Color State List Resource.
| 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
• Animation drawables
• Other drawables
See Drawable Resources.
|Drawable files for different launcher icon densities. For more information on managing launcher icons with |
|XML files that define a user interface layout. See Layout Resource|
|XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource|
| Arbitrary files to save in their raw form. To open these resources with a raw |
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the
| 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
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:
|Arbitrary XML files that can be read at runtime by calling |
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
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>