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
Let's continue with adding additional colors as the background for each new fun fact. We'll add a new Java object to hold the colors and serve one up on each tap of the button!
Color codes (from the style guide)
public String[] colors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray
};
Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
It's finally time to add
all the colors to our app.
0:00
Let's create a new class called
ColorWheel that stores our colors and
0:03
picks new ones for us.
0:07
It'll be very similar
to our factBook class.
0:09
In the Project pane, let's click on our
package name, and select New, Java Class.
0:12
Let's name it ColorWheel and hit Enter.
0:19
Now, we're going to copy and
paste our code from our factBook class and
0:24
make a few key changes, but first,
let me close the Project pane.
0:27
Let's switch over to our factBook
class and, before we copy anything,
0:32
let's delete this extra space at
the top of the getFact method.
0:36
Then let's select everything
after the word factBook and
0:40
use Cmd or Ctrl+C to copy it.
0:45
Then let's go back to
the ColorWheel class,
0:49
select everything after the word ColorWhee
and use Cmd or Ctrl+V to paste it in.
0:51
Android Studio then notices
that we're missing an import.
0:58
We didn't copy the import
statement from our factBook class,
1:01
which means it didn't get pasted here.
1:04
Hit OK to generate the import and
now we're good to go.
1:07
Instead of facts,
we'd like these to be colors.
1:11
Let's start by refactoring
facts to be named colors.
1:14
Right click on facts and
select Refactor, Rename, and
1:18
note the keyboard shortcut is Shift + F6,
and type colors.
1:23
Now I'm going to copy and paste in some
colors in the form of hexadecimal strings.
1:32
You can find these colors
below in the teacher's notes.
1:37
Okay, let's copy the array from
bracket to semicolon and then,
1:41
back in Android Studio, let's paste
it over the existing facts array.
1:45
And now that we've got all our colors,
1:49
let's make a few changes
to the getFact method.
1:52
First off, we probably shouldn't
call it getFact anymore.
1:55
Let's change it to getColor.
1:59
Since this method is never used,
that's what the gray text is telling us,
2:01
we don't need to bother
with the refactoring.
2:05
We can just rename it the old fashion way,
getColor.
2:08
Great, now the cool thing is we
don't need to change anything
2:12
about our random number generator,
we still use the Random class and
2:15
we still pick a random number based
on the length of our array up here.
2:19
Now let's go back to FunFactsActivity.
2:23
And the first thing we need to do is
create a new ColorWheel object as a field,
2:26
just like we did with factBook.
2:31
Let's add a new line at the top, below our
factBook, and type private ColorWheel,
2:33
name it colorWheel, and
set it equal to an new ColorWheel.
2:40
Now, down in our OnClick method, we can
pick a new color just like we did for
2:47
the fact.
2:51
Let's add a couple lines after we
set our fact and type String color
2:52
= colorWheel.getColor, then let's
2:59
use our new color variable as a parameter
to the setBackgroundColor method.
3:06
Which gives us an error.
3:15
If we hover over it, we see that we're
getting an incompatible types error again.
3:18
The method expects an int but
we're giving it a string.
3:22
So how are we gonna make this work?
3:26
Well, don't worry, as usual,
there is a simple solution.
3:28
We need to convert data like
this a lot in programming.
3:30
Remember, programming is primarily about
working with and manipulating data.
3:35
Let's take a short break and
then the next video, we'll fix this error.
3:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up