Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 731
xinaiz Avatar asked Jun 02 '18 19:06

xinaiz


People also ask

How do I move XML from one file to another in android?

[1] call startActivity to open another layout. startActivity(new Intent(getApplicationContext(), _second. class)); [2] Create another XML layout file which you want to display.

What is id in XML android?

ID resource is a simple resource defined in XML that provides a unique identifier for other project resources. An ID resource does not always reference an actual resource item, it is simply a unique ID that you can attach to other project resources or use as a unique integer in your app.

What is id in XML?

id. xml is generally used to declare the id's that you use for the views in the layouts. you could use something like <TextView android:id="@id/snack"> for your given xml.


3 Answers

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 :)

like image 116
Abhy Avatar answered Oct 18 '22 23:10

Abhy


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.

like image 1
Yogesh S Avatar answered Oct 18 '22 23:10

Yogesh S


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!)

like image 1
Genie Avatar answered Oct 19 '22 00:10

Genie