Search code examples
androidresourcesdrawable

Get array of id drawable resources


I need many similar drawable resources. I created the files necessary to me in the drawable folder Then the file of resources with the array:

<array name="btn_style">
    <item>@drawable/tile0</item>
    <item>@drawable/tile1</item>
    ...
</array>

But when I write

int[] btn_draw = getResources().getIntArray(R.array.btn_style);

I receive zero from the array.

If I do

  int[] btn_draw = {R.drawable.tile0,R.drawable.tile1};

All works fine.


Solution

  • If I understand your question.. here is your answer:

    try this: You can use typed array..

    <?xml version="1.0" encoding="utf-8"?>
        <resources>
    
            <string-array name="random_imgs">
                <item>@drawable/car_01</item>
                <item>@drawable/balloon_random_02</item>
                <item>@drawable/dog_03</item>
            </string-array>
    
        </resources>
    

    Then in your activity access them like so:

    TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
    //get resourceid by index
    imgs.getResourceId(i, -1)
    // or set you ImageView's resource to the id
    mImgView1.setImageResource(imgs.getResourceId(i, -1));