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
<resources>
<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">
<item>4</item>
<item>8</item>
<item>16</item>
<item>32</item>
</integer-array>
<string name="hello">Hello!</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
<array name="icons">
<item>@drawable/home</item>
<item>@drawable/settings</item>
<item>@drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
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)
//...
icons.recycle()
val colors: TypedArray = resources.obtainTypedArray(R.array.colors)
val color: Int = colors.getColor(0,0)
//...
colors.recycle()
// 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);
//...
icons.recycle();
TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);
//...
colors.recycle();