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
...
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.
Im working at a little Annotation Processor for android.
I have the following scenario:
I have a xml layout resource id and I want to find all views in this xml layout. I simply want to parse the xml layout file to retrieve some information that I will use later on.
Does anybody know if such a tool exists or how to implement something like this?
I know it's not simple. There are serval things to consider like:
same layout files in different layout-resource folders like:
res/layout/mylayout.xml
res/layout-xlarge/mylayout.xml
I want to parse both mylayout.xml files.
I only have the resource id (integer) of a layout, how do I map that back to the xml file (String, name)
Any suggestion how to start?
I doubt I can use Android classes, because I want to write an Annotation Processor. AnnotationProcessing runs in it's own jvm before compiling Android resources.
From what I understand the workflow should be as follows:
Map id (integer) to layout file name (String). I guess I have to
parse the R.java class to achieve that.
Next I have to check
recursively all layout resource folders to find the corresponding
layout.xml files.
Parsing the xml files (no big deal)
if you need the name of the resources that correspond to a particular id, you can use
String getResourceEntryName(int id)
that is a method of Resources(). Here you can find the documentation
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.
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.
I have a fairly complex android app, the contents of the 'layout' folder is becoming increasingly large. I've tried to organise the individual layout xml files into sub folders e.g. layout/dialog/, layout/activity/, layout/views/ etc. This doesn't seem to work, the content of the folders in not parsed into the R. class.
Is there a way to do this?
Thanks!
Short answer is no, subfolders are not supported. You probably just need to get clever with naming the files. See this question: Can the Android drawable directory contain subdirectories?
Resource directories should be flat. So, if your intention is to have layout/dialog, layout/activity/, layout/views/, etc. you should go with layout/dialog_whatever, layout/activity_whatever and layout/views_whatever, which gives you more or less the same organization.
No, resource directories doesn't support sub directories structures, Because it all about indexing in your R.java files,
You have to give naming conversion for your files, like, layout/activity_..
If you have more xml layout in your app then you have to give the proper naming convention as the xml use in the perticular activity.
as like if your activities are like activity1, activity2 and the xml in that activities are dialog1.xml, dialog2.xml, main1.xml, main2.xml, button1.xml, button2.xml, view1.xml, view2.xml ...etc
Then use that xml layout with naming convention as like:
layout\activity1_dialog1.xml
layout\activity1_main1.xml
layout\activity1_button1.xml
layout\activity1_view1.xml
layout\activity2_dialog2.xml
layout\activity2_main2.xml
layout\activity2_button2.xml
layout\activity2_view2.xml
Hope you got my point. It will realy help you to manage the xml layout as i am doing same thing.
Enjoy. :)