Using the New Colors3:42 with Ben Deitch
Let's fix that error! In this video we'll fix a couple errors and then set the text color of our button.
Back in the ColorWheel class, let's change getColor to return an int, 0:00 instead of a string. 0:05 And now we need to convert the string color to an int. 0:10 Right above the return statement, let's add a new int variable named color. 0:13 And let's set it equal to Color with a capital C, dot. 0:21 And then if we start typing parseColor, we can see that parseColor is a method that 0:27 takes in a colorString and returns an int, just what we're looking for. 0:32 Hit Enter to select it and add a semicolon. 0:37 The parseColor method is a handy utility method that takes a hexadecimal string, 0:41 just like we have up here, and 0:46 converts it to an integer representation that Android can understand. 0:48 So instead of returning a randomly selected color string, 0:53 let's pass that random color string into the parseColor method. 0:56 Finally, since we need to be returning an integer, 1:06 let's return our new color variable and head back to fun facts activity. 1:10 Now, instead of a string, we're returning an int. 1:17 So color should be an int, not a string. 1:21 Looks good, now let's run our app and verify it's working as we expect it to. 1:26 Okay, moment of truth, tap the button, and nice, all sorts of different colors. 1:33 There's one more thing I'd like to do here, one last color. 1:42 Notice that the color of the button text is black, but 1:46 in our mock ups, it's matching the background color. 1:50 Let's go back to our code and change this. 1:53 In the onClick method, we already have the color we want to use. 1:56 And we already have a variable for our button, showFactButton. 2:00 Let's add a new line at the bottom of onClick and add a reference to our button. 2:03 Now let's look for the method we need. 2:10 Let's type .set and we're looking for text color. 2:12 So let's add text, and then down here, 2:17 we see a method called setTextColor that takes on an int parameter. 2:20 Let's pick that one. 2:26 Then let's pass in our color variable. 2:28 And cool, but we're not quite done. 2:31 We're only changing the text color after the button has been tapped. 2:34 If the button hasn't been tapped, we'll still see black text. 2:38 We should change our button's default text color to match the default 2:42 background, green. 2:45 Let's go back into our activity_fun_facts layout file. 2:46 And make sure we're on the Design tab. 2:50 Then let's select our relativeLayout. 2:53 And copy the green color from the background property. 2:57 Then let's select our button, scroll down to the textColor property. 3:02 Paste in our color, hit Enter, and there we go, green text. 3:12 Now let's run the app to make sure everything works. 3:18 Okay, just like we expected, lots of different colors for 3:24 the background and lots of different colors for our button text. 3:28 All right, that's really coming together. 3:32 We're done with all the programming, but 3:36 there's still one little thing before we can move on to testing and debugging. 3:38
You need to sign up for Treehouse in order to download course files.Sign up