Sharing resources between projects - android

I have many projects that have an identical activity which include identical drawable resources. In each project I have duplicated the src java file, the layout xml file and the drawable images.
Ideally I'd like to be able to have one set of these and somehow share them to the other projects. Shared folders appear to be the way to go but after setting them up I cannot figure how to reference the linked files.
Lets say, to keeps things simple, I have two projects, a Source project which contains the java code file, the layout file, and a drawable folder containing all the images I require.
The second project lets call it Destination.
In Destination I create the linked folder called "linked" to the drawable folder in Source which is then viewable in explorer. In my layout xml, how can I reference the "linked" folder and then the individual images for the SRC of imageviews?
Would it also be possible to link my identical activities java source and xml files in the same manner?
If there is another or better way to accomplish this I'd be grateful for some pointers.
thanks

I think you should package your common code and resources into a library and then use this library inside all yours projects.

What you want to do is to add a folder to your project build path.
Here's how I do it in Eclipse:
Right click on the project in the Project Explorer
Properties > Java Build Path > Source > Link Source > Choose the folder
Hope this helps (:

Related

android customize drawable folder to subfolder

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

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!

Android: Refactoring XML files in Eclipse?

Is there a way to easily rename XML files in Eclipse while developing for Android so that these changes are reflected in source code?
Right-clicking on the file in package view and selecting refactor->rename changes the file's name, but does not alter references to the file within the .java source files.
I realise this is an old post, but i was trying to do the same and just came up with a solution to this in eclipse:
Search -> File Search -> Replace...
Replace:Current.Style.Name
With: New.Style.Name
Thats it! Nice and easy, i didn't want to deal with the hassle of going into all my xml layouts and changing them.
I do not think such a thing exists for Android resources directory let alone layouts. You have to change the references manually.

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?

Can you add subfolder to layout in Eclipse Android project?

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.

Categories

Resources