3

I have one peculiar problem. I have an array inside xml file:

res/values/difficulties.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string-array name="difficulty_names">
        <item> @string/easy </item>
        <item> @string/medium </item>
        <item> @string/hard </item>
        <item> @string/insane </item>
        <item> @string/preposterous </item>
        <item> @string/ludicrous </item>
    </string-array>
</resources>

For initialization of this array I used this method:

String[] difficultiesNamesArray = getResources().getStringArray(R.array.difficulty_names); 

This worked perfectly. Until I moved this file into different folder. Since then this method stopped working. New path is:

res/arrays/difficulties.xml

I would like to know, how can I locale this file and use again getStringArray() method on this relocated xml file.

Or if it is even possible to use getResources().getStringArray(R.array.***) method. If not, how can I read an array from this different path. My guess is, that it would need to be implemented the XMLParser. But I don't know how to do it with this simple array.

At developer.android.com I found this info:

String Resources

  • Define strings, string arrays, and plurals (and include string formatting and styling). Saved in res/values/ and accessed from the
    R.string, R.array, and R.plurals classes.

So I think it is somehow connected with my problem that I can't use the getStringArray() method over the file located at different path then res/values/. But this is only my hunch. I don't know.

Am I right? It is possible to read an array.xml from different path? If yes How?

Community
  • 1
  • 1
Róbert Garai
  • 53
  • 1
  • 1
  • 7
  • 3
    In general, you cannot invent new resource directories (e.g., `res/array/`). – CommonsWare Aug 05 '19 at 21:46
  • Thx for reply. I've chosen the renaming solution from Eldhose answer. Renaming the *xml file into **res/values/arrays_difficulties.xml** this sounds the cleanest way. – Róbert Garai Aug 06 '19 at 11:45

1 Answers1

3

The res/values/ is a system reserved folder. So when you add your arrays, strings etc inside that folder, an android Resource ("R") class will be generated with respect to the type of value that you are adding. So if you are using a different folder structure, then the "R" class value for the same wont be generated.

You can refer to here to understand more about the "R" class which is getting generated

If you want to maintain your values in your own folder structure, then probably you need to put it inside the "assets" folder in what ever folder structure you want. Then to access it, you need to parse the xml by your own, which is highly NOT RECOMMENDED.

You can refer to following link to see how to read a file from assets folder.

My suggestion would be to name your string file in such a way that it resembles the folder structure. In your case, consider naming as res/values/arrays_difficulties.xml

Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • 1
    Aha, now I see. Thank you for your answer Eldhose. I've read the links you provided. So I do now understand this topic of resources and R.java better than before. And the BufferedReader is unnecessary overkill for this kind of simple arraylist. I've chosen the renaming solution into **res/values/arrays_difficulties.xml** this sounds the cleanest. – Róbert Garai Aug 06 '19 at 11:43