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 need some help with this one?

Challenge Task 1 of 1

"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)?"

1 Answer

Harry James
Harry James
14,780 Points

Hey Donald!

I'd be happy to help you out here. To initiate the variable, we just need to put an = before the semicolon.

After the equals symbol, we have the declaration side - this is where we want to declare what the variable should be equal to. We also need to put our casting brace here.

So, we're setting showFactButton to be a Button, and we also need to put a cast on that side, just so that we know we're definitely getting a button, not anything else. To do that, write (Button) on the declaration side.

Ok! Now that that's out of the way, we want to say what we're referring to in our layouts. Previously, we were referring to our factTextView, but now we want our showFactButton (That's what it's called in the layout - we don't actually see this layout but we are told this in the question). So, after the casting brace, type findViewById(R.id.showFactButton). In case you were wondering, findViewById() is a general method accessible to us in any Android Activity.

The line of code you've written should look very similar to the example above in the challenge. The only things different are the type in the cast () and the id of the layout element.

Let me know how it goes! If you get stuck along the way, show me the line of code you've written and I'll tell you how to fix it for the challenge :)

Aleksandr Afanasyuk
Aleksandr Afanasyuk
449 Points

I DID IT!!!! OMG took forever like an hour in half lol But I finally solved it :D Well thanks for the hints.