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 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.