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

Just some questions about the programming that I've forgotten throughout the lesson...

I just have a few questions about the programming that were taught earlier, but forgotten.

What is the factLabel? Is a View any element on the screen (Text, button, etc.)? Can you explain (simplified more than the comment) what lines 20 and 21 do?

Hi Alex, thanks for answering. So the factLabel is the "Ants stretch..." part?

And sure, I'll clarify my other 2 questions.

Are Textboxes, buttons, checkboxes, images, (everything on a screen) all called Views?

What do lines 20 and 21 do? I know that they are explained in the comment, but can you explain it in a simpler way?

Thanks!

alex gwartney
alex gwartney
8,849 Points

Not a problem and so yea the the fact Label would be the id that is attached to the "Ants stretch..." text that shows on the screen they are two different things. The first one fact Label is used to access the text that is shown on the screen.

As far as Textboxes, buttons, checkboxes, images, (everything on a screen) goes these are not all called views the view would be the screen that you drag all of these elements on to.

Now one last thing to help answer your last question could you post the lines of code you are talking about?

Also hopefully every thing explained above makes sense if it does not please let me know and i will try to give more detailed example.

Ok, I understand now. So is there a name that all of those things are grouped under?

Here are the lines of code (I included Line 19 also, which is the comment).

// Declare our View variables and assign them the Views from the layout file
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findViewById(R.id.showFactButton);
alex gwartney
alex gwartney
8,849 Points

Also if you could to help me and others out in the future make sure to vote if you are happy with myne or others answers thank you.

alex gwartney
alex gwartney
8,849 Points

Also one other thing as well if you would like iam futhur down the line with the android track so if you have any other problems or would like to collaborate feel free to email me at alexgwartney@aol.com or you can find me on twitter @AlexGwartney would be glad to help you out.

Sure, I'm just rereading your answer to fully understand it. Once I think I've understood everything, I'll comment again with my interpretation, and you can check if it's okay, then I will vote. Thanks!

Ok... So being a complete Java beginner, I only understood a little bit of this explanation. I understand that "final" means that the element will never change, and that "TextView factLabel" means that I am creating a TextView and naming it factLabel.

However, the rest is a bit muddy. Am I just renaming the "Ants stretch..." factTextView into factLabel and setting it to "final"?

alex gwartney
alex gwartney
8,849 Points

No so what is happening is the factlable is just a id that you create for the method name that goes and access the text aka "ants stretch" The "ants stretch is already set in the design view that you type in manually from the widget plain text view that you drag on to the screen and type in the text.

alex gwartney
alex gwartney
8,849 Points

Also what other parts do you not understand ?

Ok so I am giving the "Ants stretch" part an id called "factLabel" so I can access it in the future?

alex gwartney
alex gwartney
8,849 Points

Also i suggest before you continue with the android course i would go over the java course first to get a better understanding of the basics.

alex gwartney
alex gwartney
8,849 Points

no you are giving the method aka the function fact label to acess the function latter in the code the ants stretch label has different id of factTextView that is acssed through the function final TextView factLabel = (TextView) findViewById(R.id.factTextView);

alex gwartney
alex gwartney
8,849 Points

To give you a better example i seprate this for you.

The line final TextView factLabel = (TextView) findViewById(R.id.factTextView); is a Function that has the neam factlable.

Now if you go to the design view on android studio and click on the ants stretch and go to were you set all your parameters for the text view scroll down to the id section and you will see a id of factTextView wich is used in the fact label function to access that specific fact.

You're right, I'm having a hard time understanding what some of this code is doing, I should probably learn some Java first - Thanks for bearing with me, and if you set your comment into an answer, I'll vote on it. Thank you!

alex gwartney
alex gwartney
8,849 Points

I put the main two that explain the answer the best and not a problem again if you need any help with the courses let me know.

3 Answers

alex gwartney
alex gwartney
8,849 Points

So as far as a name that they are grouped under that i know of would just be in the design view were you select them from. For instance the plain textview would be under the name of widgets.

final TextView factLabel = (TextView) findViewById(R.id.factTextView); The line above starting with the "final Keyword" Its declaring that this specific element will not change. The next few lines are just declaring a textview and naming method factlable so you can acess this latter in the code you actualy have factlable mixed up with the id that is used to acess the "the ants stretch fact" That specific id is what "factTextview" is. So back to the line of code so once you declare you factlable id you then need to cast it to Textiew and then the findveiw by id is taking and searching on The screen for the plaintextview that was set with the id of Fact text View. Once its acessed by the id you can now manipulate that data with the factlabe id. Hopefully this makes sense.

The Button showFactButton = (Button) findViewById(R.id.showFactButton); is doing the same thing as above but this is using a button which will be able to be manipulated in different ways. Let me know if you don't understand any part of what iam explaining.

alex gwartney
alex gwartney
8,849 Points

To give you a better example i seprate this for you.

The line final TextView factLabel = (TextView) findViewById(R.id.factTextView); is a Function that has the neam factlable.

Now if you go to the design view on android studio and click on the ants stretch and go to were you set all your parameters for the text view scroll down to the id section and you will see a id of factTextView wich is used in the fact label function to access that specific fact.

Ok, at a final attempt at understanding this, factLabel is a function that I can call later, and what it does is access the "Ants stretch" fact so I can manipulate it later. Is that right?

alex gwartney
alex gwartney
8,849 Points

Yes the function is to access the ants fact for latter use. If you go to my profile you can find a program that i made that uses the idea behind the fun facts app its a life quotes app that i made it will give you the i dea of what the program will do. Also once you have gone over the java course check back with this question and see if you can understand it better.