0

Is it possible to have layout IDs inside arrays.xml. I tried the following but it doesn't work:

<integer-array name="layouts_list">
        <item>R.layout.layout1</item>
        <item>R.layout.layout2</item>
        <item>R.layout.layout3</item>
        <item>R.layout.layout4</item>
        <item>R.layout.layout5</item>
</integer-array>


Any other alternatives for this ?
However, I can have an integer array inside the constants.java but just curious to know if anybody has done something similar to above.

2 Answers 2

2

The correct syntax would be

<integer-array name="layouts_list">
    <item>@layout/layout1</item>
    <item>@layout/layout2</item>
    ...
</integer-array>

but I'm pretty sure that won't work, since @layout/layout1 is not of type integer.

What would work but would require some manual extraction is this:

<array name="layouts_list">
    <item>@layout/layout1</item>
    <item>@layout/layout2</item>
    ...
</array>

You can use getResources().obtainTypedArray to get the TypedArray representing this array. Then, you can use getResourceId to get, well, the resource id. Don't forget to recycle() your TypedArray!

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

2 Comments

I feel, having the integer array in Constants.java looks much easier :)
It can be, yes. Resource arrays are super powerful when you mix strings and drawables and colors together in one array but for just integer IDs, Java is probably the easiest thing (unless you want the array to change based on resource qualifiers!)
0

Maybe you can try to get the real int in the R class and put that value in the arrays.xml.

2 Comments

NO - please don't suggest that. The R.java file is auto-generated and the resource ids can potentially change with any build. What you're suggesting would be a nightmare to support.
Thats actually true. I dont think to put values from R class in array is a good idea indeed.

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.