Android Studio changing ID's in one xml file changes the ID's in another file
Asked Answered
A

4

17

I used to give simple ids to Android Views, but lately, I stopped that altogether because of this issue.

Let's say I have 2 layouts, one named first_layout.xml and second named second_layout.xml. In each of these layouts, there is a view with id username_input. This id is used in different activity classes to access target view, be it ButterKnife binding, Kotlin Android Extensions, or even findViewById.

Now, for whatever reason, I need to rename id in just one layout. Thus I do Refactor -> Rename. What happens? Android Studio simply replaces id username_input in the whole project, messing other layouts that had view the same id. Most people don't use a unique id for each and every view in the project since it's not mandatory, but I do because I do lots of refactoring, and many times I lost hours because of this non-contextual replacement in the project.

What are my options in this case? Is there a way to make Android Studio do the contextual replacement in such a situation, or should I keep making unique ids in my project?

Antiquated answered 2/6, 2018 at 19:31 Comment(8)
Don't use refactor and do a simple copy replace? Given that most layout files are only used in one Java file directly, its barely more effort.Pardo
@GabeSechan It's not change in 1-2 files. For example when including common layout in the xml file, it might concern even 10-15 files, and changing all that by hand? =/ also Kotlin Android Extension use view's id as reference name in java class, and if there are many operations performed on the target view in the code, it might concern tons of occurences.Antiquated
@xiniaz Then you need to improve your implementation- if you're referencing subfields of a layout in a dozen files, then you ought to be converting it into a fragment or compound view with an interface that abstracts all of that for you. I'd say if you're referencing it in more than 1 you should be doing that.Pardo
I suggest changing the name of the question to "chaning id in one place changes id in other places in android xml file" - I am quite sure many people will be running into this problem. Please see my workaround answer below.Bani
This also happens when you copy one layout into another and try to refactor the id in the new file... it tends to refactor the id in source file also...Bani
Did you find a solution?Conto
I already answered this question for you on this post.Shawn
Possible duplicate of Changing ID in one place changes ID in other places in Android XML fileShawn
H
4

Moments ago I was facing this same issue and I found it to be the Android Studio's expected behavior, as other elements might be constrained to the element whose ID is getting changed. So you need to update all the IDs particularly in your new layout file through its text editor AND NOT from the Attribute section of its design editor Its working fine for me, and so must work well for you too :)

Hershey answered 14/9, 2019 at 10:9 Comment(1)
I believe That is NOT expected behavior. if this bug is annoying for you too, please mark star on this report: issuetracker.google.com/issues/141508081Grieg
D
1

I too faced same issue, when i copied widget from an one activity to another activity and tried to change ID in copied widget. It prompt a dialog box and i clicked "No (Local Only)". But, i feel no use of that option. it still updated ID in old activity too.

Workaround:

Try to change ID or Attributes through Code(Text Editor) not from Design.

I feel google should address this issue and remove this bug.

Defensible answered 7/9, 2020 at 4:46 Comment(0)
R
1

Right click on the id you want to rename the go to:

Refactor -> Rename

Now you will see an option "Scope" and under Scope you need to select "Current File" in order to rename the id in that file only. (Not sure if it was there when this question was asked!)

Redhanded answered 29/9, 2021 at 21:56 Comment(0)
B
0

I just came across this post because I was having a similar problem. I have an app with different flavors. In one of my layouts I have 12 buttons. In a different flavor, I want all of those buttons plus I add 4 more. So, I created a new version of the layout in a flavor-specific directory. The problem is every time I tried to change anything that has an ID it would change the identically-named layout in the main directory.

With flavors, you're supposed to be able to override the main directory's layout with a flavor specific one. This was extremely frustrating!

This MUST be a bug in Android Studio. The frustrating part is that it's still not fixed. I installed Android Studio via the JetBrains toolbox since I use other JetBrains IDEs. Currently I'm on version 3.5.3

My workaround

To get around this bug I created a "test" project. I copied the layout code that I wanted to modify, pasted it in the test project and edited it there. When I was done altering it the way I wanted, I created the alternative layout in the flavor specific directory (using the same name as the main directory's layout that I want to override) of the actual project I was working on. Then I copied the layout from my test project and pasted it into the flavor specific file. By doing this, it didn't alter the layout in the project's main directory.

My thoughts

You shouldn't have to do this. It's certainly not the intuitive way. This is why I can't believe that this is anything but a bug. With that said, I hope I have provided a clear workaround for anyone else who's stuck. If I haven't comment below and I will clarify what I can.

Buckinghamshire answered 13/2, 2020 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.