1 00:00:00,160 --> 00:00:01,500 We're almost done. 2 00:00:01,500 --> 00:00:04,810 The last thing we need to do is pick a new background color 3 00:00:04,810 --> 00:00:07,000 each time we display a new fact. 4 00:00:07,000 --> 00:00:08,790 Sounds simple enough, right? 5 00:00:08,790 --> 00:00:12,840 If you're feeling adventurous, why don't you pause me and try it yourself. 6 00:00:12,840 --> 00:00:15,200 Then come back and see how I do it. 7 00:00:15,200 --> 00:00:16,380 Here are the steps you need to do. 8 00:00:17,640 --> 00:00:22,260 Step one, declare a property with the data type of RelativeLayout? 9 00:00:22,260 --> 00:00:24,080 And set it equal to null. 10 00:00:24,080 --> 00:00:26,840 The format is just like our other view fields but 11 00:00:26,840 --> 00:00:29,640 with a different name and data type. 12 00:00:29,640 --> 00:00:35,210 Step two, set this property in the onCreate method just like the other views. 13 00:00:35,210 --> 00:00:38,450 You may need to add an ID to the RelativeLayout in the layout file. 14 00:00:39,650 --> 00:00:42,550 Step three, where we set a new fun fact, 15 00:00:42,550 --> 00:00:46,460 call the setBackgroundColor method on this new variable. 16 00:00:46,460 --> 00:00:48,898 Use Color.RED as the parameter. 17 00:00:51,056 --> 00:00:53,190 All right, did you get it working? 18 00:00:53,190 --> 00:00:56,730 Let's switch to FunFactsActivity and start from the top. 19 00:00:56,730 --> 00:01:00,172 Declare a property with the data type of RelativeLayout? 20 00:01:00,172 --> 00:01:07,043 Let's add a new line below our showFactButton declaration, 21 00:01:07,043 --> 00:01:13,792 and type private var_relativeLayout: RelativeLayout? 22 00:01:13,792 --> 00:01:14,980 And set it equal to null. 23 00:01:17,310 --> 00:01:19,380 Okay, on to step two. 24 00:01:19,380 --> 00:01:21,450 Set this property on the onCreate method. 25 00:01:22,570 --> 00:01:26,167 Let's add a new line below where we initialized showFactButton. 26 00:01:26,167 --> 00:01:32,272 And then type relativeLayout 27 00:01:32,272 --> 00:01:38,868 = findViewById(R.id.). 28 00:01:38,868 --> 00:01:42,480 And that's right, we haven't give our relativeLayout and ID yet. 29 00:01:42,480 --> 00:01:45,200 If we had, we will be be able to see that ID in this list. 30 00:01:46,400 --> 00:01:48,250 Let's open of our layout file. 31 00:01:48,250 --> 00:01:54,994 So Project > res > layout > activity_fun_facts.xml. 32 00:01:54,994 --> 00:01:59,275 Let me have the preview and inside this first RelativeLayout tab, 33 00:01:59,275 --> 00:02:01,860 let's add an ID property. 34 00:02:01,860 --> 00:02:08,429 Let's add a line after where we set the padding and 35 00:02:08,429 --> 00:02:16,884 type android:id="@+id/relative " with a lower case r. 36 00:02:19,085 --> 00:02:22,197 Remember, we could have also set the ID from the design tab by 37 00:02:22,197 --> 00:02:25,190 selecting the RelativeLayout from the component tree and 38 00:02:25,190 --> 00:02:27,340 then setting the ID on the property pane. 39 00:02:28,860 --> 00:02:32,090 Looks good, let's go back to our activity and reference our new ID. 40 00:02:34,310 --> 00:02:38,480 Let's hit Ctrl+Space, to bring back up auto complete. 41 00:02:40,060 --> 00:02:42,090 And then select relativeLayout. 42 00:02:42,090 --> 00:02:44,690 And finally, add the cast to relativeLayout. 43 00:02:45,840 --> 00:02:47,280 So much for step two. 44 00:02:47,280 --> 00:02:51,620 Next step, step three, where we set a new fun fact called the set 45 00:02:51,620 --> 00:02:56,790 background color method on our new variable using Color.RED as the parameter. 46 00:02:56,790 --> 00:02:59,180 This is referring to our onClick listener. 47 00:02:59,180 --> 00:03:04,586 At the bottom, let's reference our new variable and 48 00:03:04,586 --> 00:03:09,646 assert that it isn't null, relativeLayout!! 49 00:03:09,646 --> 00:03:14,570 Then, using auto complete as our guide, let's add a dot and type sbc. 50 00:03:15,650 --> 00:03:17,720 Just typing the first letter of each word and 51 00:03:17,720 --> 00:03:21,340 a method is another way to let auto complete know what we're looking for. 52 00:03:22,480 --> 00:03:25,270 Let's select the setBackgroundColor method and 53 00:03:25,270 --> 00:03:27,990 it takes in an into color as the parameter. 54 00:03:27,990 --> 00:03:31,020 For this example, we're just using a system color. 55 00:03:31,020 --> 00:03:35,600 Inside the parenthesis, let's type the class name, Color with a capital C. 56 00:03:35,600 --> 00:03:41,064 And hit Enter to import it and then add dot RED. 57 00:03:41,064 --> 00:03:43,466 All right, let's give it a shot. 58 00:03:46,156 --> 00:03:48,367 Now, when we first tap the button, 59 00:03:48,367 --> 00:03:52,940 it should change the background color to red and it does, cool. 60 00:03:52,940 --> 00:03:54,160 Let's pause for a moment and 61 00:03:54,160 --> 00:03:57,045 then we'll make some changes to use a new color for each fact.