In Android , one of the important factor determining the performance and reliability of android application is its resources. The awareness regarding this important aspect is some time overlooked by even experience developers and designers. In this short tutorial we will take a look at what are android resources and how they are managed in Android system.
Android Resource files
Android allows you to create static resources like images and XML configuration files. This allows you to keep these resources separate from the source code of your Android application.
Resource files must be placed in the /res directory of your application in a predefined sub-folder. The specific sub-folder depends on type of resource which is stored. As a developer you can also add and update additional qualifiers to the folder name to indicate that the related resources should be used for special configurations. These are termed as resource qualifiers. For example, you can specify that layout file is only valid for a certain screen size.
Resource | Location | Description |
---|---|---|
Drawables |
/res/drawables |
It includes Images (e.g., png or jpeg files)or vector drawables or XML files which scale automatically with the density of the Android device. |
Simple Values |
/res/values |
Used to define strings, colors, dimensions, styles and static arrays of strings or integers via XML files. By convention each type is stored in a separate file, e.g., strings are defined in the res/values/strings.xml file. |
Layouts |
/res/layout |
XML files with layout descriptions are used to define the user interface for activities and fragments. |
Styles and themes |
/res/values |
Files which define the appearance of your Android application. |
Animations |
/res/animator |
Defines animations in XML for the animation API which allows to animate arbitrary properties of objects over time. |
Raw data |
/res/raw |
Arbitrary files saved in their raw form. You access them via an |
Menus |
/res/menu |
Defines the actions which can be used in the toolbar of the application. |
Resource files and R.java
Every relevant resource in the res folder, gets an ID assigned by the Android build system. Android generates gen a R.java file which contains the generated values. These references are static integer values.
If you add a new resource file, the corresponding reference is automatically created in a R.java file. Manual changes in the R.java file are not necessary and will be overwritten by the IDE in which you are working. The Android system provides methods to access the corresponding resource files via these IDs.
For example, to access a String with the R.string.yourString
ID in your source code, you would use the getString(R.string.yourString)
method defined on the Context
class.