8

I want a drawable id array of integer values which I can store like an integer-array in res/values/XXX.xml by using integer-array tag. Below is integer-array declared in strings.xml

<integer-array name="icons">
     <item>1</item>
     <item>2</item>
     <item>3</item>
     <item>4</item>
</integer-array>

But I want to store drawable image ids like @drawable/someImage as an integer array in xml.

OR Is there any alternatives to store drawable integer ids as an integer array in xml.

1

3 Answers 3

22

I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:

First, integer-array in res/values/arrays.xml:

<integer-array name="frag_home_ids">
    <item>@drawable/frag_home_credit_return_money</item>
    <item>@drawable/frag_home_transfer</item>
    <item>@drawable/frag_home_balance</item>
    <item>@drawable/frag_home_charge</item>
    <item>@drawable/frag_home_finance_cdd</item>
    <item>@drawable/frag_home_finance_ybjr</item>
    <item>@drawable/frag_home_more</item>
</integer-array>

Second, get resource integer values programmatically:

TypedArray tArray = getResources().obtainTypedArray(
            R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
    ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller. 
//After calling this function you must not ever touch the typed array again.
tArray.recycle();

Third, call the integer values like this:

holder.iv.setImageResource(ids[position]);

Of course, you can get integer values of string, color, integer, layout, menu...in this way.

I hope these codes will inspire you.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to recycle the typed array at the end of the second step by calling tArray.recycle(), to avoid memory problems.
4

Take a look at documentation, specifically More Resource Types article. Quote:

Typed Array
A TypedArray defined in XML. You can use this to create an array of other resources, such as drawables.

EXAMPLE:
XML file saved at 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>

Comments

1

You can use a string array.

Extract:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="media_names">
        <item>Big Buck Bunny</item>
        <item>Elephants Dream</item>
        <item>Sintel</item>
        <item>Tears of Steel</item>
    </string-array>

    <string-array name="media_uris">
        <item>http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4</item>
        <item>http://archive.org/download/ElephantsDream_277/elephant_dreams_640_512kb.mp4</item>
        <item>http://archive.org/download/Sintel/sintel-2048-stereo_512kb.mp4</item>
        <item>http://archive.org/download/Tears-of-Steel/tears_of_steel_720p.mp4</item>
    </string-array>
</resources>

What is it that you want to achieve, I cannot 100% tell you if this is the best choice for you.

2 Comments

@Knossos-i want drawable images to store as an array in xml file so i can use them directly in my adapter.
Then you want to take a look at @aga's answer. That will do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.