3

I have the following XML

<string-array name="str_arr1">
  <item>My item 1</item>
  <item>My item 2</item>
  <item>My item 3</item>
</string-array>

<string-array name="str_arr2">
  <item>My item 4</item>
  <item>My item 5</item>
  <item>My item 6</item>
</string-array>

How can I reference the above strings arrays using an array.. something like below (maybe another type of array needs to be used?)

<string-array name="my_strings_arrays">
  <item>R.array.str_arr1</item>
  <item>R.array.str_arr2</item>
</string-array>

Basically in the code, I want to read the my_strings_arrays and then for each array , I want to grab the list of items within.

2 Answers 2

3

In any xml resource file we can use reference of array/string/integer/color/etc. with "@" prefix. So, here we can use @array for getting reference of another array str_arr1 and str_arr2.

<string-array name="str_arr1">
  <item>My item 1</item>
  <item>My item 2</item>
  <item>My item 3</item>
</string-array>

<string-array name="str_arr2">
  <item>My item 4</item>
  <item>My item 5</item>
  <item>My item 6</item>
</string-array>

You to need change your code as below.

<string-array name="my_strings_arrays">
  <item>@array/str_arr1</item>
  <item>@array/str_arr2</item>
</string-array>
Sign up to request clarification or add additional context in comments.

Comments

2

Your second array should be an array of string arrays, not string array of string arrays.

I think the way you would do what you need is:

<array name="my_strings_arrays">
    <item>@array/str_arr1 </item>
    <item>@array/str_arr2 </item>
</array>

3 Comments

hmm interesting, can you update the answer with how you can load such thing using code? For example, how to display item1? Thank you so much
Actually, here's a good example already instead of me typing it here.
@Snake no problem, glad to help :)

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.