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. :)
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.
I have doubts regarding the following ones. please clarify it.
1. What is the problem if i set the text of text view in xml file instead of java file(String.xml)?
2. What is the usage of SP? Give me one example.can anybody helpme.
thanks
Regarding your first question: strings.xml file is used for storing string resources, especially for the sake of resolving the localization issues. You can store multiple strings.xml files inside your project in different folders depending on the localization. This makes translating your application to a different language easier: you just create another strings.xml file, translate all the strings and put it inside the corresponding folder. Hope this helps.
There is already a similar question , but I am not satisfied with the answer
Can I put layout directory's xml file in different subdirectories in Android?
because there are too many xml files , if they are not into different group ,I can not find the specific one that I want .
Do you guys have any different method to solve this problem such as virtual group or something ?
As you can read in the linked question, and in the questions linked to this question, it is not supported to have custom folders inside the folders supported by Android.
You could clean up your files by following naming conventions, like layout_xyz.xml, image_abc.png, ...
I use the namespaceing technique,
i.e.
addnewrecord_main.xml
addnewrecord_custombutton.xml
addnewrecord_newbutton.png
search_listviewitem.xml
search_bgimage.png
etc
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
...
Is it possible to create subfolders under res/layout and place the layout XML files there so that one can call a view like setContentView(R.layout.questions.create); or setContentView(R.layout.questions/create); ?
From my tests, no.
You might want to consider a naming convention:
questions_create
questions_list
answers_list
Or, investigate Android library projects: http://androidblogger.blogspot.com/2010/09/android-library-projects.html - seems pretty good to add more structure.