Android studio items copied cannot be refactored because they are linked - android

I have had to do a lot of copying and pasting of my layouts and items in them. If I copy and paste these and then try to refactor their names they also change the names of all of my copies. This happens in the xml code editor or in my design editor. Could someone tell me what this is called and how can I copy and paste without having the items be linked? I am so frustrated. Thanks!
For example if I have a textview with and Id of "tv" and then I copy the block of xml and paste it elsewhere to use the same block and then refactor to rename it's id then both the original and copy will update. I want to paste a version of the textview that is NOT linked like this.

When you write android:id="#+id/foo" in your layout file, the system will wind up generating an int value in a file called R.java, something like R.id.foo = 0x7f123456.
When you then write android:id="#+id/foo" again in a different file, that will reference the same R.id.foo int. So, when you use the refactoring tools, all instances of #id/foo are considered.
Perhaps you can use the simple textual Find/Replace action instead of Refactor -> Rename.

The copies aren't "linked". They just use the same names, so if you try to refactor the copy, then Android Studio finds that name in the original and will rename it. Instead, you will have to edit your copies manually without using the Android Studio refactoring tools.
Another option if you are using version control is to do the rename refactor then undo the local, uncommitted changes in the original files with Ctrl+Alt+Z.

Related

How to rename (Refactor) xml element in Android Studio without affecting other activities

In my Android mobile app project, I am using Android Studio as an IDE. I have multiple activities there. Multiple of activities have xml element with the same name ("myTextView"). These are absolutely different xml textViews located in different layouts for different not related to each other activities. The only common thing is these textViews have similar id = "myTextView".
I try to rename xml element in xml layout for one activity. Say, in activity_layout1.xml I would like to rename "myTextView" to "someonesTextView". Android Studio automatically makes it via refactoring.
However, during refactoring, it renames elements with the same id (myTextView) in all the other activities. Finally, I have "someonesTextView" in other activities though they are not related.
How can I avoid that?
In that case, you don't have to refactor the id but instead edit it from the Split or Code view of your xml file. This is because when you refactor or modify the id from the Design View, it modifies all other TextViews having the same id.
This is from my own personal experience since I faced what you are facing...
You can set a scope for the Rename refactoring (idea 2020.3). For example, close all tabs and open only activity_main.xml and MainActivity.java files. Then, select id in xml (Refactor -> Rename) and in the Rename popup dialog under "Scope" select "Open Files" in order to rename the id in those files only.

How do I refactor string resources in strings.xml in Eclipse/ADT?

I'd like to rename 100+ strings in my app (to append com.myapp. to the front of all of them for example), but the refactor menu is grayed out when right-clicking. Any other ways of achieving this? Incidentally, does the new Android studio have this functionality?
I already do know the paste to Excel trick for alphabetizing everything.

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.

How to rename widget ID's in Eclipse?

I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.

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