resources in android

Tut#4 Android XML Resources Part 1

What is a resource in Android?

A resource in Android is a file (like a sound file or a file that describes the layout for a window) or a value (like the title of a dialog box) that is bound to an executable application. These files and values are bound to the executable in such a way that you can change them or provide alternatives without recompiling the application.

resources in android

 

Well, since that is a pretty exhaustive list, lets take a breather and take one resource at a time to try and understand why, when, how and where to use each

Resources: Values

  1. Strings: Place them inside res/values/<any-filename>.xml. String resources allow Java-formatted strings and raw HTML in addition to simple strings. These resource IDs are accessible in Java code as R.string.*. The XML node for strings is a <string> with a name attribute
  2. Colors: Place them inside res/values/<any-filename>.xml. Store different color hex codes and access them in code through R.color.*
  3. String Arrays: Place them inside res/values/<any-filename>.xml. Store an array of Strings under the <string-array> tag with a name. Store each String from the array within an <item> tag and refer to the String array as a whole using R.array.* in Java code
  4. Dimensions: Place them inside res/values/<any-filename>.xml. Represents dimensions or sizes of various
    elements or views in Android. Supports pixels, inches, millimeters, density independent pixels (DP) , and scale independent pixels (SP). Refer to them in Java code as R.dimen.* and store the respective values under a <dimen> tag with a suitable value given to its name attribute
  5. Plurals: A special set of Strings that deal with quantity. For example, in English, we say 1 egg and many eggs or 2 eggs to represent a collection of eggs.  Notice the use of ‘s’ when the quantity under consideration changes from singular to plural. Different languages have different ways to interpret single and multiple quantities as well as definite and indefinite number of quantities and this resource allows us to to work with that. Place them inside res/values/<any-filename>.xml and access them in Java code as R.plural.*