6. Resources

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.

The following table gives an overview of the supported resources and their standard folder prefixes.

Table 1. Resources
Resource Folder Description
Drawables /res/drawables 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 theres/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 InputStream object.
Menus /res/menu Defines the actions which can be used in the toolbar of the application.

a. 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.javafile. Manual changes in the R.java file are not necessary and will be overwritten by the tooling. 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.

Android activities define their user interface with views (widgets) and fragments. This user interface can be defined via XML layout resource files in the <filename class=”filename”>/res/layout_ folder or via Java code. You can also mix both approaches.

Defining layouts via XML layout files is the preferred way. This separates the programming logic from the layout definition. It also allows the definition of different layouts for different devices.

A layout resource file is referred to as layout. A layout specifies the ViewGroups, Views, their relationship and their attributes via an XML representation.