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 (retired 2014) Learning the Language Objects and Random Numbers

Paul Yorde
Paul Yorde
10,497 Points

ID to get a RelativeLayout object just like we get a TextView and Button.

Building a simple android app > learning the language > extra credit

So, are these:

  1. answerLabel
  2. getAnswerButton

Objects or variables in the following:

final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

I have tried to make a RelativeLayout object like this:

getBackgroundColor = findViewById(R.id.bgColor);

However, I'm not sure what to set parameter type or the object type (if this even the way I should be making an object?

5 Answers

Jason Wiram
Jason Wiram
42,762 Points

To answer the first part of your question about the following code:

final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

answerLabel is a variable of object type TextView and getAnswerButton is a variable of object type Button

This may hint toward your solution. Assuming you did as the instructions say and added an ID to the RelativeLayout element in activity_main.xml (it appears you used 'bgcolor' as your ID, then your java code is close but missing components.

getBackgroundColor = findViewById(R.id.bgColor);

Your code has a variable name, but no object type. As with the original code creating TextView and Button objects, you need to create a RelativeLayout object. This would look as follows:

RelativeLayout getBackgroundColor = (RelativeLayout) findViewById(R.id.bgColor);

Also notice that because the findViewById returns an object of type ID, you need to cast it as a (RelativeLayout) type to store in your variable 'getBackgroundColor'.

You will now be able to use RelativeLayout methods on your variable like setBackgroundColor() from the instructions:

getBackgroundColor.setBackgroundColor(Color.GREEN);

You can add this to your onClick() method to make the color change when that event happens.

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Hey Paul,

First of all, have you tried to define the getBackgroundColor as you did on the TextView and Button? The thing is, if Eclipse say so, remeber to cast items on the getBackgroundColor naming, so it can correct it.

But... As i see you want to change background color. For that i recommend going to your_activity.xml and adding these

        android:background="@color/white"

for other colors check for hexadecimal ones

i hope it helps

Paul Yorde
Paul Yorde
10,497 Points

Thanks so much Jason--just what I was looking for!

Jason Wiram
Jason Wiram
42,762 Points

No problem. Glad it helped.

I'm also trying to do the same thing, but I can't figure out how to construct the 'ID' in the 'activity_main.xml'. I don't quite understand why I need to create a 'RelativeLayout' object and set its color to 'GREEN' when I'm trying to change the background color of the 'button' object.

Joshua Friedman
Joshua Friedman
537 Points

Same here. It it just a matter of typing the following:

<RelativeLayout
android:id="@+id/backgroundColor"
/RelativeLayout> 

In the .xml file, then calling that function in the main activity file? I tried that, then tried to manage my imports and nothing happened. I looked in the graphical editor and also didn't find anything there,