Creating a list of integers in XML for android
I would like to create a list of integers in the / res folder of an android project. However, I want these integers to point to resources in / res / raw. So, for example, I would like something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="built_in_sounds">
<item>@raw/sound</item>
</integer-array>
</resources>
But the id doesn't look like this, how can I do this, is there a way to do this? Or should I just create a list in the java class?
thanks
+2
a source to share
3 answers
And the correct answer is actually TypedArray
The documentation provides examples of resource lists:
<?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>
And the code to extract the values:
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors = res.obtainTypedArray(R.array.icons);
int color = colors.getColor(0,0);
+1
a source to share
To do this in XML without a database see http://developer.android.com/guide/topics/resources/more-resources.html#Integer
0
a source to share