Suppose if I am using a same id for two different views in two different layouts, I can see only one refernce is created for the id in the class “id” in R.java. Actually what I think is that it should show an error when trying to add the constant with the same name in the class “id” on build .Why its not showing an error?.And how it identifies two different views with a single id?
Share
If you have two different people, in 2 different groups, both named Vicky, it does not matter who that person is when calling the name (=id). If you call ‘Vicky’ in the first group, the Vicky in THAT group responds.
In android, you assign a ‘group’ of views to the activity by using
setContentView(<layout file>)where<layout file>is the group.If you request a view by findViewWithId() with a id which is not available (as in, not in the loaded layout file), it returns null.
example:
layout1.xml contains
layout2.xml contains
Calling
findViewById(R.id.name1)on the first and second layout file, will return the first view. But when you callfindViewById(R.id.name2)on the 2nd layout, it will returnnull. The id DOES exist in A layout file, but simply not in the ‘loaded’ layout file.Id’s are not references to View objects. They are identifiers that can be used by multiple Views in different layouts. By looking for an identifier in the layout file, you can acquire a reference to the View using it.