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 Adding the onClick() Method

Sadok Cervantes
Sadok Cervantes
6,204 Points

Exactly where is the factTextView being updated to display the new fact?

I'm having a hard time understanding the code.

TextView factsLabel = (TextView) findViewById(R.id.factTextView);

is assigning the value of the factTextView object to a new variable called factsLabel so that we can work on it.

We then update it in factsLabel.setText(fact);

But where's the code that actually updates BACK the object factTextView so that we see the new fact in the APP?

Thank you!!

1 Answer

Ratul Sarna
PLUS
Ratul Sarna
Courses Plus Student 11,618 Points

Here's my 2 cents.... R.id.factTextView is not actually an object itself. It is an int type variable that holds the id equivalent to the id of the factTextView defined in the xml file. When you initialize factsLabel TextView, it basically inflates the xml element of factTextView into a Java object that holds all the relevant information inside itself including which xml element it is attached to. So, when we can factsLabel.setText(), it is updating the Java object we have created which in turn updates the xml element because of which we see the change reflect visually in the activity.

I hope I didn't confuse you more with the answer!

Sadok Cervantes
Sadok Cervantes
6,204 Points

Thank you Ratul. So by "inflates" do you mean something like entanglement? What happens to one happens to the other? I guess when you assign an XML ID to an object and then you update that object, it updates all instances of that ID? Am I correct? Thanks!

Ratul Sarna
Ratul Sarna
Courses Plus Student 11,618 Points

"inflates" means that a Java object is created. This happens when we call setContentView(R.layout.x).

This method "inflates" objects for each element in the layout xml file. These objects have a hierarchy similar to the XML hierarchy starting with the root element like "RelativeLayout". So, the object for TextView is also created in this method.

Then when we call findViewById(R.id.y) and assign it to a TextView variable, we are asking our root view/element object to look for a View in its hierarchy with the given ID. It looks for it and returns a reference to it, which we in turn assign to a variable (in your case, "factsLabel"). The returned reference is a View and not a TextView which is why we cast it to one.

Now, this TextView variable,"factsLabel", refers to an object that holds information about the TextView XML element with the given ID, like we store information in an object in member variables. And if I'm correct, the ID has to be unique in a single XML file. Other files can have elements with the same ID but they won't be updated because they do not belong to the same layout hierarchy.

Check out this link....it describes what I wrote above in a good way too. Layout Inflation