I've come across some android native code where the strings.xml in android would be named strings-global.xml and likewise the styles.xml would instead be called styles-global.xml. how is android identifying this ? what does it mean ?
The naming of files within the values\ folder is arbitrary. What resources the file provides is defined by the contents of the file itself, not the name. Which resources are used in a given configuration is determined by qualifiers on the values\ folder (e.g. values-fr), not the file name.
You could even mix and match resource types in a single file if you really wanted to, though I recommend following convention.
From the Providing Resources documentation for the values\ folder:
Because each resource is defined with its own XML element, you can
name the file whatever you want and place different resource types in
one file. However, for clarity, you might want to place unique
resource types in different files.
In this case, it sounds like the developers discovered that their styles.xml was getting too large and/or they found that they had a few distinct categories of styles that they wanted to separate, and thus put them in different files to keep them more organized.
Related
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-arrays 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).
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.
It is already well established that dimensions, colors, and other resource related values should be stored in their respective XML file. However, is this still appropriate when creating a library? Defining these resources in a library's XML file would perhaps bring confusion to anyone using the library. Are there are recommended practices for defining resources in a library project?
yes one giant one .. prefix the resource name both in the id and the xmlfile name as resources with same names often clash with parent stuff that has the same name
I plan on making my app bilingual, English and German.
I have two folder within /values for both, and now all of a sudden none of my strings can be found, giving me the following error for all my strings
"Error: No resource found that matches the given name (at 'text' with value '#string/stringname')
How can I tell it to look in my new folders where all the strings are?
you don't need 2 folders inside values. In values you keep your strings.xml file (from here it will take the strings for english) and you create another folder, inside res/ called values-de where you will create a copy of the strings.xml file and just translate the content. The android system will choose the right content, depending on the language used on the phone.
Must the resource ID's for views in XML layouts be unique across all layouts?
For example, I'm working on a little recipe manager app. I have a layout file for adding a new ingredient. In this layout I have an EditText for the ingredient that I'd like to call "edt_name". But I'm afraid that this name is too general; e.g. I might also have an EditText for a recipe name, a cooking procedure name, etc in other XML layout files.
However, I also don't want to make the labels more complex than necessary. I'd like to avoid calling the aforementioned EditText "edt_name_new_ingredient" if I could.
I'm curious as to how developers organize their resources in general. Android doesn't support sub-directories for resources as far as I know, so naming schemes can get really messy.
No, resource ID should not be unique across different xml layouts however they must be unique in a particular xml file.
Resource IDs are namespaced within the package. When you access a resource (in XML, for example), the package name is implicitly set to the current one. You can have other resource files in a different package and refer to those within your code or XML (this is how one accesses the platform resources that come with the SDK).
Similarly in code, you can access a different package's R class and use its resources, but all those within the same package must have unique names.
More info can be found in the documentation here.
I am newbie to Android and playing with some Hello World codes.I observed that android put every resource i.e. image,string etc in res folder and we access it like #drawable/icon i.e icon image in drawable folder or like R.layout.main which means main.xml inside layout folder.
But while accessing strings we use #string/string_name but we dont specify its parent folder name i.e.values.Why syntax differs for strings ?
It may sound silly but it makes to think and put this question.
All resources of the same type in android is in a flat hierarchy.
You don't specify a directory name but instead the type of the resource.
Even if you split all strings between different files it will always be #string/string_name I'm afraid.
A good thing you can do to get some structure is to do something similar to this:
#string/error_network_io
#string/error_network_unknown_host
#string/message_save_successful
...