Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Basic Android Programming Making a Button Do Something

what actually happens if i removed the line 20 (TextView)

in the line 21, i know that we added it to make a sound for the button. but in the line 20, i can't figure why and what it's for, i removed it and i ran the app and it worked fine. please explain it in another way.

2 Answers

Hello,

If the line 20 you're referring to is:

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

What it is doing is giving is finding the item in our view by the given identifier(which was defined in XML), and creating a variable for it that we can interact with in the code. If we didn't have that line, we wouldn't be able to run any code involving that button. So this way we have something we can actually use to set the onclicklistener.

Michael Waihura
Michael Waihura
6,963 Points

Hi Mohammed.

Line 20 : TextView factLabel = (TextView) findViewById(R.id.factTextView); Removing this line does not affect running the app as it has not yet been used yet.We've only declared the TextView and initialised it by referencing the value from xml.It actually looks greyed out as it has not yet been used yet.Am sure we'll use it in the upcoming videos.