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.
Related
I have notice that Android Studio doesnt update changes made to layout and it always displays old layout design. so i went to check layout folder in explorer and notice that it has three different layout folder namimg : layout , layout-v17 and layout-v20. I have notice that both layout-v17 and 20 contains old layout and was never updated with new layout design. so i have deleted those two folder and it started working . Is it a good idea to delete them
If you didn't create spcial versions of your layouts for particular api versions, it is ok that you deleted the folders.
That kind of folders are used to keep resources for specific api versions. E.g. you can have an activity_main.xml layout in layout folder and in layout-v17 folder. Then, the layout from the default folder would be used for devices with api level < 17 and the layout from layout-v17 for sevices with api level 17 and higher.
Is there other way except manually create all the layout folder like "layout-small,layout-large,layout-xlarge" ? Is there any automated function in android studio can automatically create these folder while creating the project?
As per my suggestion,there is no need for creating different size of layouts because we have a library by which we can set all dimensions according to devices.You can get the answer from my previous answer here.
Sub-Folder I didn't mean the drawable-hdpi, drawable-mdpi, drawable-ldpi, etc
but #drawable\myfolder\img.png
I'm a bit concerned on customizing the folder structure in drawable similar the way we have package to structure the java files.I'm using too many images for my project and when I try to copy activity layout xml; searching for the images in drawable, I felt it boring! Does android have such feature or some wayout?
I think, currently this way exists. I also have many drawables and layouts, so tried to start from layouts and then added drawables and menus. I recommend to see Can the Android Layout folder contain subfolders? and http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/.
Yes, I have now new resources in new subfolders. It requires time to manage (create res folders, edit build.gradle), but a folder tree becomes more neat. Sometimes AS cannot find resources during compilation. In this case I have to create new folders and edit build.gradle. Probably after several weeks everything will be done.
UPDATE
It has worked until Android Studio updated to 3.2 (and 3.2.1). Currently if you move any drawable, layout, menu resource to another folder (and add this folder to build.gradle as written in the articles above) you cannot normally use it. Before 3.2 we could simply press Build > Rebuild Project and reference to that resource. Now they have broken this behaviour and you should press File > Invalidate caches / Restart... > Just Restart (or close and open AS) to access this drawable as usual. If you don't want to restart AS, you can use the resource, but write a path to it manually like #drawable/reset_password, AS won't hint as you type and won't draw it in Design tab.
If you use Kotlin Android extensions and reference to ids like send_button (without findViewById()) you will get so many bugs that can't imagine. If you change resources, often nothing changes in layouts until you rebuild the project. This is because Kotlin caches resources. I often forget about it and waste hours.
As far as i know, NO.
There was an interesting post on G+ with a workaround for the layout dir, that works also for the drawables dir. I guess.
All the infos are here
Android doesn't support subfolders within its predefined directories, the only thing possible is for you to create directories within the res directory
Well, I was wondering whether I could load different views from same application for different android versions. I thought that I could create an activity that can call one of the two separate activities defined for different versions of android via intent. I mean suppose that in one activity I have used action bar which is available from ice cream sandwich only, and other could possibly to use views in earlier android releases.
1) For layout that support multiple screens you can refer this
2) If you want to load different layout depending on different android version then you can create different layout xml files and then in the activity, for example below code
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
// only for android older than gingerbread
//call setContentView with your layout
}
3) You can have layout in layout-v(version-number) folder inside your res folder. Where for example for SDK 11 the folder name will be layout-v11
Yes. Create a layout-vX folder in your res, where X is the version number (11 for Honeycomb, for example). Any file in there will override the version of the file in the layout directory if using version X or higher.
if all you need this is for the action bar , i highly recommend using actionbarsherlock library .
you can also do the checking of the version using code (read here) :
if(VERSION.SDK_... <VERSION_CODES.... )
and by setting the qualifier of the version , as written here , so for example, for resources that need to be used on API 11 and higher , put the qualifier v11 .
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?