Resources are external files (non-code files) that are used by your code and compiled into your application at build time. Android supports a number of different kinds of resource files, including XML, PNG, and JPEG files. The XML files have very different formats depending on what they describe.

Resources are externalized from source code, and XML files are compiled into a binary, fast loading format for efficiency reasons. Strings are compressed into a more efficient storage form.

List of resources

Resource-types and where to place them:

• styles, strings and arrays ^ "/res/values/"

o Names do not have to be exactly like: o 'arrays.xml' to define arrays o 'colors.xml' to define colors

■ #RGB, #ARGB, #RRGGBB, #AARRGGBB o 'dimens.xml' to define dimensions o 'strings.xml' to define strings o 'styles.xml' to definestyle objects

• raw files like mp3s or videos ^K/Tes/raw/"

Using resources in code

Using resources in code is just a matter of knowing the full resource ID and what type of object your resource has been compiled into. Here is the syntax for referring to a resource:

R.resource type.resource name or android.R.resource_type.resource_name

Where resource_type is the R subclass that holds a specific type of resource.resource_name is the name attribute for resources defined in XML files, or the file name (without the extension) for resources defined by other file types. Each type of resource will be added to a specific R subclass, depending on the type of resource it is.

Resources compiled by your own application can be referred to without a package name (simply as R.resource type.resource name). Android contains a number of standard resources, such as screen styles and button backgrounds. To refer to these in code, you must qualify them with android, as for example in: android.R.drawable.button background.

Referencing Resources

A value supplied in an attribute (or resource) can also be a reference to another resource. This is often used in layout files to supply strings (so they can be localized) and images (which exist in another file), though a reference can be any resource type including colors and integers.

For example, if we have color resources, we can write a layout file that sets the text color size to be the value contained in one of those resources:



layout width="fill parent"


layout height="fill parent"


textColor="@color/opaque red"


text="Hello, World!" />

Note here the use of the prefix to introduce a resource reference -the text following that is the name of a resource in the form of @[package:]type/name. In this case we didn't need to specify the package because we are referencing a resource in our own package. To reference a system resource, you would need to write:

<EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/opaque_red" android:text="Hello, World!" />

As another example, you should always use resource references when supplying strings in a layout file so that they can be localized:

<EditText android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/opaque_red" android:text="@string/hello_world" />

0 0

Post a comment