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.
Related
The problem is: I haven't done the huge project before (with bunch of activities and layouts). I don't know how the structure should be organize especially for layouts. I try to create folders and put it there but it doesn't work, just underlines elements. I create folders in this way: layout -> new -> directory (see bellow on the screen)
The question is: how to organize layouts? I don't want to have list of all my project layouts in one folder 'layout'.
They all have to be in the 'layout' folder (can be also in 'layout-land', etc).
Subdirectories not allowed.
If this or that part of a layout repeats often in other layouts you can use <include> xml tag.
I must create 2 folder layout and layout-v11. Inside, there is 5 files, but only one change.
Must I put all files in the folder, ore can just put in v11 the file who changes?
You only need to put the files that are different.
You should have the layout folder (without any numbers) and put all the common files there. Only files that are different per version should go to folders with version numbers, e.g. layout-v11.
This way you don't need to have the same content in 2 places and it's easier to maintain.
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. :)
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 have an application and putting all of the layouts inside of the res/layout folder is getting pretty large and hard to manage. I would like to have folders for all the layouts. I have read that there cannot be subdirectories inside the layout folder but that there could be in the assets folder. So my question is, how do I access a file and set it as my view from a file in the assets directory? Something like the following
int assetId = getAssets().open("main.xml");
setContentView(assetId)
Would the above code work? How would I set that xml file for my view?
Thanks.
You can achieve this by using a custom script and having it run before the build executes. Android seems to ignore anything in layout subdirectories, so you can safely put your files into them. The following ruby script (written for Linux, but easily convertible to other platforms) will then delete everything that's not a directory in res/layout/ and copy every file from the subdirs into res/layout/:
#!/usr/bin/ruby
require "fileutils"
def collect_files(directory)
FileUtils.cd(directory)
FileUtils.rm(Dir.entries(directory).reject{|x| File.directory?(x)}) #Remove all layout files in base dir
files_to_copy=Dir.glob("**/*").reject{|x| File.directory?(x)}
files_to_copy.each{|x| print "Copying #{x} to #{directory}\n"}
FileUtils.cp(files_to_copy, directory) #Copy all files in subdir into base dir
end
if ARGV[0]!=nil && File.directory?(ARGV[0])
xml_dir=ARGV[0]
layout_dir="#{xml_dir}/layout"
collect_files(layout_dir)
else
puts("Must specify a valid directory!")
end
Be warned that the above script is not robust, and will actually delete any layout files not in a subdirectory. You can always remove the deletion step if you like, but then any files you remove from the subdirectories will remain in the main directory for subsequent builds.
If you're running Eclipse, you can then configure an external tool, which you can add to your builders later. Just open up Run -> External Tools -> External Tools Configurations, and create a new tool under 'Programs'. Here a screenie of my settings:
Note: The working directory is a red herring, and won't be used. You'll need to specify the location where you drop the script, not the one shown here
Now you can add the tool to the builders for your project. Select your project and open up Project -> Properties. Now Select the 'Builders' item and click 'Import'. You should see your tool there if you defined it successfully. It needs to run before the rest of the build process, so make sure to move it up to the top of the list. Here's what it should look like when you're done:
Now you just move layout files into subdirectories (but watch out for name collisions, remember the files will all end up in the same directory for the build!) and build your project. You'll see them magically appear in the root of /res/layout/ when you do this and your app should then build normally.
Caveat Scriptor: If you're specifying multiple layouts, or anything else which uses more than just the /res/layout/ directory, you'll need to extend this script or add the tool multiple times for the different directories to handle it. I don't personally use this technique, and so haven't seen where it falls down, but have performed a test with a basic android Hello World app with a couple of layouts in some subdirectories.
Also, my script will break if used with paths containing spaces!
The short answer: it can't be done in that way.
The main reasons:
In the res/layout folder all .xml files are precompiled, so Android can use them as resources. In assets folder all files remain intact, so the app can read them as regular files. (In your code example you get InputStream, not resource ID).
Android automatically manages layouts located in the res/layout folder, searching the best matched to current screen resolution, orientation, locale, etc.
layoutinflator might help you
look here
What does LayoutInflater in Android do?
I don't think this is a good idea, and probably just doesn't work. What would the framework do if it thought it needed an hdpi version of your layout, for example?
You might try to go after the root cause of your layout proliferation:
Could you develop better naming conventions for your layouts?
Could you refactor certain layouts so that more components can reuse the same layouts?
Are you manually handling orientations instead of relying on -portrait and -landscape?