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

Angus McNab
Angus McNab
2,487 Points

Why is a button treated as a variable?

On the Fun Facts Activity one of the first tasks is to set up your variables.

The factLabel I understand as it will change but not quite sure why the button is set as a variable.

3 Answers

Hi Angus,

Don't mistake the final as making the button immutable - the Button's properties can all be altered but that variable will only ever point to that Button object; i.e. it links the variable code to the graphical button object. That's done initially by the line of code you have selected and it is done on a once-and-for-all basis.

We declare the button as a variable because we want to use its functionality. Without the variable, we can't use its click events or, like you say, easily change the text colour. I'm guessing the TextView that hasn't got a variable assigned is just a static label that never alters?

Make sense?

Steve.

Angus McNab
Angus McNab
2,487 Points

Yes, thanks. Much appreciated.

Angus McNab
Angus McNab
2,487 Points

Sorry, forgot to add, yes, the TextView without an assigned variable is just static text.

Hi Angus,

A button is not as static as you might think. It has many properties that you can access, set and retrieve. For example, you can disable it or enable it by setting those properties of that particular button. You can click it, too!

All these things are specific to that button which is why having a variable to handle these things is useful.

Have a look at the properties that a button has - you can see them all in Android Studio on the right hand side in the Layout window where you drop the button.

I hope that helps!

Steve.

Angus McNab
Angus McNab
2,487 Points

Thanks. Specifically, what is the following code doing:

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

Do we only declare it as a variable in Fun Facts because we eventually change the button text colour?

Full code is this:

//Declare our View variables and assign them the views from the layout file
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        final Button showFactButton = (Button) findViewById(R.id.showFactButton);
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

But there is another TextView on the screen ("Did you know?") and we don't declare that as a variable. Why not?

Filip Łubniewski
Filip Łubniewski
2,178 Points

Hi Angus,

We don't declare that TextView as a variable because we won't be changing properties of it, so declaring it would be redundant.