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 trial

Android

Gonçalo Palma
Gonçalo Palma
9,121 Points

Why can't we just add an ID programmatically to the view?

In past videos (or courses), it was said that the state of a view, eg. an EditText field, is automatically saved and restored by android when the app is saved.

So, I ask here: why can't we just assign a new ID with "check.setId(i);" ?

Thank you for your time!

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Gonçalo!

You can add an ID programmatically. It's just that usually we create our Views in XML, and so it's just easier to set the ID in XML as well.

Gonçalo Palma
Gonçalo Palma
9,121 Points

Oh that's great!

I was almost suspicious of my solution since it felt like "too good to be true".

Thanks a lot for the time answering the question! :D