>This page defines more types of resources you can externalize, including:Bool,Color,Dimension,ID,Integer,Integer Array,Typed Array
XML file saved at> EXAMPLE:res/values-small/bools.xml
:<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="screen_small">true</bool> <bool name="adjust_view_bounds">true</bool> </resources>This application code retrieves the boolean:
Resources res =getResources()
; boolean screenIsSmall = res.getBoolean
(R.bool.screen_small);
res/values/dimens.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="ball_radius">30dp</dimen> <dimen name="font_size">16sp</dimen> </resources>
This application code retrieves a dimension:
Resources res =getResources()
; float fontSize = res.getDimension
(R.dimen.font_size);
This layout XML applies dimensions to attributes:
<TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/>> EXAMPLE:
res/values/integers.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="bits"> <item>4</item> <item>8</item> <item>16</item> <item>32</item> </integer-array> </resources>
This application code retrieves the integer array:
Resources res =getResources()
; int[] bits = res.getIntArray
(R.array.bits);
>
res/values/arrays.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <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>
This application code retrieves each array and then obtains the first entry in each array:
Resources res =getResources()
; 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);