Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The last thing we need to do is pick a new background color each time we display a new fact. Try it yourself, then watch this video for my explanation.
Read Me!
Android recently updated the findViewById
method to be generic. This means, in most cases, that a cast (e.g. (RelativeLayout)
) is no longer necessary. If you don't see an error when using findViewById
, that just means you're on a newer version of Android than I am :)
Documentation
- RelativeLayout Class Reference
- Color Class Reference - Contains several Color constants you can use
We're almost done.
0:00
The last thing we need to do
is pick a new background color
0:01
each time we display a new fun fact.
0:04
Sounds simple enough, right?
0:07
If you're feeling adventurous, why
don't you pause me and try it yourself?
0:09
Then come back and see how I do it.
0:13
Here are the steps you need to do.
0:15
Step 1, declare a field or member variable
with a data type of relative layout.
0:17
The format is just like our other View
fields, but with a different name and
0:23
data type.
0:27
Step 2, Set this field in the onCreate
method just like the other Views.
0:29
You may need to add an ID to
the RelativeLayout in the layout file.
0:33
Step 3, where we set a new fun fact,
0:38
call the 'setBackgroundColor()'
method on this new variable.
0:40
Use Color.RED as the parameter.
0:44
All right, did you get it working?
0:48
Let's switch to fun facts activity and
start from the top.
0:50
Declare a field with the data
type of RelativeLayout.
0:54
Let's add a new line below our
showFactButton declaration and
0:57
type private RelativeLayout and
let's name it RelativeLayout.
1:01
Okay, on to step two.
1:07
Set this field and the onCreate method.
1:09
Let's add a new line below where
we initialized showFactButton.
1:12
And then type relativeLayout =,
1:16
then a cast to a relativeLayout,
followed by findViewById.
1:20
And then in parenthesis, type R.ID.
1:28
And that's right, we haven't given
our relative layout an ID yet.
1:32
If we had,
we'd be able to see that ID in this list.
1:37
Let's open our layout file, so
open the project pane, Res Directory,
1:41
layout Activity fun facts and
then I'll close this directory again.
1:45
And inside this first relative layout tag,
let's add an ID property.
1:52
Let's add a line after padding and
1:57
type android:id= and
then in quotation marks,
2:02
"@+id/relativeLayout with a lowercase r.
2:08
Remember, we could have also
set the id from the design tab
2:15
by selecting the relativeLayout
from the component tree and
2:18
then setting the id in the property pane.
2:21
Looks good.
2:23
Let's go back to our activity and
reference our new id.
2:24
Let's hit Ctrl+Space to
bring autocomplete back up.
2:27
And then select Relative Layout.
2:31
And add a semicolon.
2:35
So much for step two.
2:38
Next up, step three, where we set
a new fun fact called the setback
2:39
onColor method on our new variable
using color.red as the parameter.
2:44
This is referring to the onClick method.
2:49
At the bottom, let's add a reference
to our new variable, RelativeLayout,
2:52
and then using auto complete as our
guide let's add a dot and type sbc.
3:01
That's right, just typing the first
letter of each word in a method
3:08
is another way to let auto complete
know what we're looking for.
3:12
So let's select
the setBackgroundColor method, and
3:16
it takes an int color as a parameter.
3:19
For this example,
we're just using a system color.
3:22
Inside the parentheses,
let's type the class name,
3:25
color with a capital C,
and then add .capital RED.
3:29
All right, let's give it a shot.
3:33
Now when we first tap the button, it
should change the background color to red.
3:38
And it does, cool.
3:44
Let's pause for a moment and then we will
make some changes to use a new color for
3:46
each fact.
3:50
You need to sign up for Treehouse in order to download course files.
Sign up