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) Improving Our Code Dynamically Changing the Background Color

For learning, using different variations of "relativeLayout" is BAD. The line of code uses it 4 times! cant follow that.

The line:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); Is impossible for someone learning to follow.

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

I agree it may be a little hard to follow but the procedure is the same as the 2 lines right above where you define the Button and the TextView.

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

I don't know if you mean you didn't know what each relative layout meant so I have broke it down below numbered 1 - 4 as if reading left to right.

  1. RelativeLayout - This is the Data type like String, int, char etc etc
  2. relativeLayout - This is the name of the variable, you could of called this whatever you want just make sure whenever Ben refers to relativeLayout as a variable name you use whatever you called the variable.
  3. (RelativeLayout) - This is called casting you must cast the layout to the correct type as the findview returns a generic View.
  4. (R.id.relativeLayout) - This is the id of the layout as defined in the XML file you set in the onCreate() method. you can also call this id whatever you want, if you go into the XML file there should be a line android:id="@+id/relativeLayout" change the name here to whatever you wish like android:id="@+id/myNewLayout" you would then change (R.id.relativeLayout) to (R.id.myNewLayout).

I think the reason for the 4 Relative Layouts is because it is a quick fix by the sound of it, bear in mind that number 1 and 3 RelativeLayout's are compulsory to define the code correctly

Hope this helps (if you weren't quite sure)

Daniel