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!

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 Initializing a Button

I Don't understand what the id is and it would be?

"Initialize the showFactButton variable using the findViewById() method like the TextView above it. The ID for the button is showFactButton, and don't forget to cast it to a Button with (Button)!"

what would it be?

1 Answer

Rebecca Rich
Rebecca Rich
Courses Plus Student 8,592 Points

Hi Donald,

The findViewById method simply finds a view element with a given id in your Android views. The show fact button has an id of showFactButton, so we can use findViewById to get a reference to that view element, generically. To make sure we access that view element as a Button (so we can use all of the Android Button methods on it), we will cast it to the Button type:

showFactButton  = (Button) findViewById(R.id.showFactButton);

The R is a reference to the application resources and the dots represent that we are taking a step into those resources, similar to opening up a file folder... So within the resources, we are looking within the id's for the showFactButton id.

Hope this helps! Let us know if you have any more questions.