layout-v4 and layout-v11? Should I duplicate files? - android

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.

Related

Android: Modify xml from resource and asset

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.

In Android Studio, can layout xml files be grouped into folders?

In Android Studio, can layout xml files be grouped into folders?
Able to do it with the java files by creating different packages/folders, can the layout files be similarly grouped/organized?
Looking for ease of organization in my workflow when i have a ton of xml files to work with.
No, the files have to be in predefined folders.
Here is a link to more information on available folder names:
http://developer.android.com/guide/topics/resources/providing-resources.html
Actually, they can!
Although the steps are a bit too many, it is indeed possible to group your xml files in folders. Just follow the following steps:
Switch to Project view; you can now see all your project folders and sub-folders properly.
Backup all your layout files; at this stage you'll find them all under android/app/src/main/res/layout.
Delete the entire layout directory (android/app/src/main/res/layout). Remember to properly backup all your layout files before this step.
Right click the res directory and select "New" and then select "Directory".
Give it a name; remember the name must be entirely lowercase. Your new directory will now appear under the res folder. Let's call this directory layouts for instance.
Right click your new directory (layouts) and select "New" and then select "Directory". This way, we are creating a new sub-directory. We can give it any name we want, just remmber it must be completely lowercase.
You can repeat step 6 as many times as you want and keep creating sub-directories.
VERY IMPORTANT! Right click any of the sub-directories and select "New" and then select "Directory". YOU MUST NAME THIS DIRECTORY layout.
Repeat this for all sub-directories.
Move all the backed-up xml files (in step 2) to the layout directory of the folder you want to put them in.
Add the code below to your build.gradle (app) file:
sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/layout_for_fragment',
'src/main/res/layouts',
'src/main/res'
]
}
}
Replace layouts with your sub-directory name and add as many sub-directories as are available.
Sync project with gradle files
And that's it.. Pretty herculean but it can come in handy anytime. You may want to visit this link for more clarification.
I hope this helps.. Merry coding!

Load layout file based on platform version in my case

I know I can create /layout-v7, /layout-v8, /layout-v11 folders to allow my app to load the suitable layout for different platform.
BUT, the above way needs me to create different layout folders for all needed platforms.
I would like to have only two layout folders, if my app is running on platform with API version >= 11 , it loads from layout-x/, otherwise load layout files from layout-y/.
How to achieve this?
I would like to have only two layout folders, if my app is running on platform with API version >= 11 , it loads from layout-x/, otherwise load layout files from layout-y/.
Create res/layout-v11/ and res/layout/. And you're done.
I had this issue as well. The answer above prompted me to change the way I was doing it. I had a folder called 'layout-v8' and 'layout', thinking that if it matched to v8 it would use that one, and any other folder would match the regular layout.
So if you find it using the wrong folder, switch the way you handle the folder names. In this example, I created a 'layout-v11' which is v11 and greater, then my normal 'layout' folder is 10 and below.

Multiple layers of folder inside the layout

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.

Is it possible to create a view from an xml file in the assets directory?

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?

Categories

Resources