In my android project, I have a folder called layout to store all xml files for layout, but there is also a folder inside called layout_settings
layout---->layout_settings--->about.xml
In my code
I have setContentView(R.XX.about). How do I set it such that reflect the about.xml inside layout_settings folder.
The reason I am doing is I don't want to scatter all the layout files into flat hierarchy.
No, you can't configure or modify a folder structure in android packages res/layout because they are read only file system.
The resources mechanism doesn't support subfolders in the layout directory, so you need to keep that hierarchy flat.
If you have to do like that just you can give a xml files name like, layout_setting.xml , layout_display.xml etc and use it.
Related
How to create user defined folder in Android studio for example to create mylayout folder in res folder and Also to create xml file same name as i created in layout folder. mean duplicate file I want to create xml file with the same name but the message show to that xml file must be unique.
You can't do it, the only time you can create duplicate layout files is when they are within predefined folder names. res/layout-v14 for API 14+, res/layout-v21 for API 21+, etc and nowhere else.
As I read from several website, editing xml stored inside res/ folder not possible
But I really need to modify the xml inside res/ folder, for example I have myxml.xml inside res/xml/
If editing it not possible, so is there any way to modify it?
Is that possible doing this:
Delete res/xml/myxml.xml then Copy a new one from asset folder?
Rename res/xml/myxml.xml to myxml.xml.old then Make a new one?
How about edit xml file inside assets folder? Is that possible?
Logic :
There's an app that reads myxml.xml from res/xml/ instead of having 2 different apk with different preferences. I just want to merge them to one and make an option, when the option is save off course it will need to modify myxml.xml inside res/xml/
Details
Actually some preference saved inside myxml.xml, the preference is
<iconmask img1="iconmask" />
<scale factor="0.75" />
I just trying to edit iconmask and scale factor value
You do not need to edit neither in the res nor in the assets folder. This will be really hard work to do and probably will not work on all devices systems as I imagine you would need to change file permissions and operate with file system commands directly.
Additionally these changes would be overriden once you decide to update your app!!
I rather advise you to change your logic - on app install copy the default xml in the internal app storage. Use this xml from then on. There you can modify whenever you like. Also refering to files located there is also very easy.
I always get a resource overwriting warning when I want to create a layout file BUT in different folder (for different configuration). I created a folder in lib directory as layout-port (for portrait orientation) and after right clicking the very same folder,If i wanna create a new file with the name of default xml layout file, it gives me the "file overwriting warning". WHY IS THAT? As far as I know I can use same file name in different resource folder.
I encountered this annoying situation too, it seems that even you selected layout-land to create the new file there, it still defaults to layout, so my workaround is to create the new file with a different name, for example main1.xml. Then I cut/paste the file from layout, into the layout-land.
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?
When I create a subfolder in the layout folder, and drag an xml file to it, the generated R file doesn't seem to show the subfolder. a) is there a way to change that b) is the file and file structure still picked up by SVN and c) so, if I do it that way, can I still just refer to the object using layout?
When I create a subfolder in the
layout folder, and drag an xml file to
it, the generated R file doesn't seem
to show the subfolder
That is because that is not supported by Android. You cannot have subfolders of resources.
PHP_Jedi's advice, though, is good (e.g., svn:ignore).
Its all automatic.
a )The generated R class only contains static integers for each item in your layout.
b) What is picked up by svn is all up to you, but I usally put the gen folder to svn:ignore
c) if you put the gen folder and the R.java into svn you could get problems since a old version of the file can be checked in/out of the respository. This will happen if you are part of a team. So, put svn:ignore on the gen folder.