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 Build a Simple Android App (2014) Basic Android Programming Accessing Views in Code

Why do we add our code after the call to setContentView?

Specifically, why after that method, instead of the other auto generated methods?

1 Answer

Louis Sankey
Louis Sankey
22,595 Points

I think the answer is onCreate gets called before the other auto generated methods we can override. It is important for the content view to be set early on because much of the code we write in our activity is dependent on knowing which layout file we are referencing. For example, if we were to try to reference a TextView in our code before setting a specific layout file, the system would not be able to recognize exactly where to look. This way we can also have the same id for a TextView in two separate layout files, but the activity knows which layout file to look in.