Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKeivan Norouzi
4,513 PointsUsing same ID over different layouts??
We are using same ID, "iconImageView" as an instance in different layouts. So would it cause any issue and isn't it better to avoid this? Also, how findViewById recognize to assign the appropriate one each time we invoke it?
Thanks :)
2 Answers
Kevin Faust
15,353 PointsLook at the oncreate method in any activity. Do you see how we are setting the layout of our activity? (R.layout.layoutnamehere). When we do findViewById, we are looking for the approriate id in that layout xml :)
Boban Talevski
24,793 PointsThanks Kevin, I was wondering the same thing as Keivan.
And Abhinav, I was also wondering the same thing as you, but came to a conclusion that it's probably better as Ben does it in this very project. I guess having two separate IDs for hourlyTemperatureLabel and dailyTemperatureLabel for each layout file is a valid option though. However, in this very same project, we have member variables in different classes which are named exactly the same (mTime, mSummary, mIcon in Day and Hour classes).
And that doesn't add to the confusion because they are private and have getters and setters and all that. So, it's best to think of those layout view IDs as being private (to some extent) and they can have the same name as view IDs in a different layout file just as the private member variables of two different classes can have the exact name, but being member variables of a different class. We would probably need to be careful though to not have an ID name collision with the IDs in the "master" layout which has a listview (or recyclerview) with the IDs used in the list item layout which is to be used by the list in the "master" layout.
And also, in favor of using the same IDs in different layout files, this came to mind. During code completion, when you write R.id. ... it is more convenient to just start typing temp... or time... than having to prefix that with hourly..., daily... etc.
Keivan Norouzi
4,513 PointsKeivan Norouzi
4,513 PointsThanks Kevin :) , It helped a lot!
Abhinav Kanoria
7,730 PointsAbhinav Kanoria
7,730 PointsThis means the ID 'temperatureLabel' of the layout 'daily_list_item.xml' is different from the same ID name of the layout 'hourly_list_item.xml'?? Would it not be better if I assigned different ID names to them, such as 'hourlyTemperatureLabel' (for displaying houry forecast), 'dailyTemperatureLabel' (for displaying daily forecast) and 'currentTemperatureLabel' than to just assign one generic ID name 'temperatureLabel'?? Would this not create any confusion?