Issue
I'm attempting to create a 'Preferences' Activity for my Wear OS app (home-baked as I don't believe the standard Settings Activity copes with round screens).
In order to support the round screen I am planning to use a WearableRecyclerView
and so need to define string-array
s for the contents of the Recycler layouts.
To keep things clean in my code, I'd like to keep these string-arrays out of my strings.xml files if possible.
Therefore, is it possible to use, for example, preferences.xml
in the res/values
folder (and provide translations in the values-??
folders) and then reference this in code?
I have tried creating preferences.xml
but when I try to retrieve the arrays with
String[] prefsTitlesArray = getResources().getStringArray(R.preferences.prefs_titles);
I get an error flagged in the IDE as 'preferences' isn't recognised under R.
Do I have to stick to the standard .xml
file names such as strings.xml
and array.xml
or is it possible to use an arbitrary file name under the values tree to keep thinsg nice and clean and obviously named?
(Note, I have looked at Is it possible to create translateable arbitrary XML resources in Android Studio? which seems to imply that arbitrary xml file names might be possible outside of the values tree, but doesn't mention how they are referenced in code (Java, in my case).
Solution
As per Mike M's comment, yes it is possible to name the XML resource files anything you want as the code reference R.????.itemName
is derived from the item type not the file it comes from.
So a file called prefs.xml
could contain <string name="itemName">
items and <string-array name="itemName">
items etc and they will be referenced from code as R.string.itemName
and R.array.itemName
.
The XML filename itself is irrelevant so long as it is saved in the correct folder within the project for value resource files.
Answered By - Fat Monk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.