Boolean Resources
XML-defined logical values used within a app.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.bool.<flag_name> - Reference (XML):
@[package:]bool/<flag_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_compact_mode">false</bool>
</resources>
The <bool> element stores either true or false. Its name becomes the resource identifier.
Usage in code:
Resources res = getResources();
boolean compact = res.getBoolean(R.bool.is_compact_mode);
val compact: Boolean = resources.getBoolean(R.bool.is_compact_mode)
Usage in layout:
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="@bool/is_compact_mode" />
Color Resources
Define colors in hexadecimal notation inside XML. Formats supported:
-
#RGB,#ARGB,#RRGGBB,#AARRGGBB -
Storage:
res/values/<any_name>.xml -
Reference (Java):
R.color.<color_name> -
Reference (XML):
@[package:]color/<color_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="vivid_green">#0F0</color>
<color name="semi_blue">#800000FF</color>
</resources>
Code access:
Resources res = getResources();
int vivid = res.getColor(R.color.vivid_green);
val vivid: Int = resources.getColor(R.color.vivid_green)
Layout usage:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/semi_blue" />
Dimension Resources
Represent size quantities with units. Supported units: dp, sp, pt, px, mm, in.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.dimen.<dim_name> - Reference (XML):
@[package:]dimen/<dim_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="btn_height">48dp</dimen>
<dimen name="label_textsize">14sp</dimen>
</resources>
Code retrieval:
Resources res = getResources();
float height = res.getDimension(R.dimen.btn_height);
val height: Float = resources.getDimension(R.dimen.btn_height)
Layout application:
<Button
android:layout_height="@dimen/btn_height"
android:textSize="@dimen/label_textsize" />
ID Resources
Declare unique identifiers for views or logical items.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.id.<identifier> - Reference (XML):
@[package:]id/<identifier>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="confirm_btn" />
<item type="id" name="warning_dialog" />
</resources>
IDs serve as markers; they do not represent actual resources.
Assigning in layout:
<Button android:id="@id/confirm_btn" />
Using in code:
showDialog(R.id.warning_dialog);
showDialog(R.id.warning_dialog)
Matching in dialog creation:
protected Dialog onCreateDialog(int id) {
if (id == R.id.warning_dialog) {
// build dialog
}
return null;
}
override fun onCreateDialog(id: Int): Dialog? {
return if (id == R.id.warning_dialog) { /* build dialog */ } else null
}
Integer Resources
Store whole numbers in XML.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.integer.<num_name> - Reference (XML):
@[package:]integer/<num_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="retry_limit">3</integer>
</resources>
Access:
Resources res = getResources();
int limit = res.getInteger(R.integer.retry_limit);
val limit: Int = resources.getInteger(R.integer.retry_limit)
Integer Array Resources
Define arrays of integers in XML.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.array.<arr_name> - Reference (XML):
@[package:]array/<arr_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="prime_nums">
<item>2</item>
<item>3</item>
<item>5</item>
<item>7</item>
</integer-array>
</resources>
Retrieval:
Resources res = getResources();
int[] primes = res.getIntArray(R.array.prime_nums);
val primes: IntArray = resources.getIntArray(R.array.prime_nums)
Typed Array Resources
Create arrays holding mixed resource types via TypedArray.
- Storage:
res/values/<any_name>.xml - Reference (Java):
R.array.<arr_name> - Reference (XML):
@[package:]array/<arr_name>
Structure:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="ui_icons">
<item>@drawable/icon_home</item>
<item>@drawable/icon_profile</item>
</array>
<array name="theme_colors">
<item>#FFFFFFFF</item>
<item>#FF000000</item>
</array>
</resources>
Usage in code:
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.ui_icons);
Drawable homeIcon = icons.getDrawable(0);
icons.recycle();
TypedArray themeColors = res.obtainTypedArray(R.array.theme_colors);
int bg = themeColors.getColor(0, 0);
themeColors.recycle();
val icons: TypedArray = resources.obtainTypedArray(R.array.ui_icons)
val homeIcon: Drawable = icons.getDrawable(0)
icons.recycle()
val themeColors: TypedArray = resources.obtainTypedArray(R.array.theme_colors)
val bg: Int = themeColors.getColor(0, 0)
themeColors.recycle()