Values overview

In res/values folder you can define following types of resources:

  • Bool - XML resource that carries a boolean value
  • Color - XML resource that carries a color value (a hexadecimal color)
  • Dimension - XML resource that carries a dimension value with a unit of measure
  • ID - XML resource that provides a unique identifier for application resources and components
  • Integer - XML resource that carries an integer value
  • Integer Array - XML resource that provides an array of integers.
  • String - XML resource that carries an string value
  • String Array - XML resource that provides an array of strings.
  • Typed Array - XML resource that provides an array of other resources, such as drawables.

The <resources > is used as the root element for XML value files.

Usually, each kind of resource is stored in a separate XML file, for example, colors.xml, strings.xml, dimens.xml, etc.

Declaring values
    <bool name="screen_small">true</bool>
    <color name="opaque_red">#f00</color>
    <color name="translucent_red">#80ff0000</color>
    <dimen name="textview_height">25dp</dimen>
    <dimen name="textview_width">150dp</dimen>
    <dimen name="ball_radius">30dp</dimen>
    <dimen name="font_size">16sp</dimen>
    <item type="id" name="button_ok" />
    <item type="id" name="dialog_exit" />
    <integer name="max_speed">75</integer>
     <integer-array name="bits">
    <string name="hello">Hello!</string>
    <string-array name="planets_array">
    <array name="icons">
    <array name="colors">

You can load values programmatically from any context object.

Always recycle a typed array when you no longer need it.

Loading values programmatically
// loading within activity
val color: Int = resources.getColor(R.color.opaque_red)
val maxSpeed: Int = resources.getInteger(R.integer.max_speed)
val bits: IntArray = resources.getIntArray(R.array.bits)
val string: String = getString(R.string.hello)
val array: Array = resources.getStringArray(R.array.planets_array)

val icons: TypedArray = resources.obtainTypedArray(R.array.icons)
val drawable: Drawable = icons.getDrawable(0)

val colors: TypedArray = resources.obtainTypedArray(R.array.colors)
val color: Int = colors.getColor(0,0)
// loading within activity
Resources res = getResources();

int color = res.getColor(R.color.opaque_red);
int maxSpeed = res.getInteger(R.integer.max_speed);
int[] bits = res.getIntArray(R.array.bits);
String string = getString(R.string.hello);
String[] planets = res.getStringArray(R.array.planets_array);

TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);