Using the New Colors3:38 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, 0:00 let's change getColor to return an Int instead of a String. 0:02 And now we need to convert the string color to an Int. 0:08 Right above the return statement, let's add a new val named colorAsInt. 0:12 And let's set it equal to Color with a capital C and enter to import it dot. 0:19 And then if we start typing parseColor we can see that parseColor 0:26 is a method that takes in a colorString and returns an Int. 0:31 Just what we're looking for. 0:36 The parseColor method is a handy utility method 0:38 that takes a hexadecimal string just like what we have up here. 0:41 And converts it to an integer representation the Android can understand. 0:46 So now instead of returning a randomly selected colorString, 0:50 let's pass that random colorString into the parseColor method. 0:54 So we'll pass in colors at index randomNumber. 0:59 Finally, since we need to be returning integer, let's return our new color as int 1:05 variable, And 1:10 head back to FunFactsActivity. 1:15 Now instead of a string, we're returning an int, and everything looks good. 1:19 Now let's run our app and verify it's working as we expect it to. 1:24 Okay, moment of truth. 1:29 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:38 Notice that the color of the button text is black, but 1:43 in our mockups, it's matching the background color. 1:47 Let's go back to our code and change this. 1:51 In our OnClickListener, we already have a color that we want to use and 1:53 we already have a variable for our button, showFactButton. 1:58 Let's add a new line at the bottom of our OnClickListener and 2:01 add a reference to our button. 2:04 Then let's assert that it's not null. 2:07 And then let's look for the method we need. 2:10 Let's type .set and we're looking for text color so let's add textcolor. 2:12 And then right here, 2:18 we see a method called setTextColor that takes in an Int parameter. 2:19 Let's click that one. 2:24 Then, let's pass in our color variable. 2:26 Cool, but we're not quite done. 2:29 We're only changing the text color after the button has been tapped. 2:32 If the button hasn't been tapped, we'll still see black text. 2:36 We should change our button's default text color to match the default 2:40 background, green. 2:43 Let's go back into our activity_fun_facts layout file and 2:45 make sure we're on the Design tab. 2:49 Then let's select our relativeLayout and 2:52 copy the green color from the background property. 2:54 Then let's select our button, scroll down to the textColor property, 2:59 Paste in our color and hit Enter. 3:10 And there we go, green text. 3:13 Now let's run the app and make sure everything works. 3:16 Okay, just like we expected. 3:21 Lots of different colors for the background and 3:24 lots of different colors for the button text. 3:26 All right, the app's really coming together. 3:30 We're done with all the programing but 3:33 there's still one little thing before we can move on to testing and debugging. 3:34
You need to sign up for Treehouse in order to download course files.Sign up