Repeated id’s in different xml layouts on Android

Asked

Viewed 32 times

2

I am developing app and noticed that I can put the same id in two different xmls. For example:

Imagine a Textview.

In the 1st Layout I define your id:

<TextView
        android:id="@+id/textViewExemplo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" XML 1"
 />

the 2nd Layout I define your id:

<TextView
        android:id="@+id/textViewExemplo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" XML 2"
 />

That is, I set the same ID to different textViews each contained in a different layout.xml file.

I don’t have trouble with compilation. But I want to know if there is any problem doing this way and if this can harm the functioning of the application in any way?

1 answer

2


The method findViewById() just search for the id in the view hierarchy it belongs to, like this:

  • in different xml’s no problem.

  • In the same xml you can use but should avoid. There can be no repetition within the same Viewgroup, since the method will always return and only the first one you find. You must use the findViewById() reference to the Viewgroup where the View is located.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.