1 00:00:00,630 --> 00:00:04,840 Awesome now it's time to assign values to these new variables. 2 00:00:04,840 --> 00:00:08,260 Let's start by adding four new lines to the bottom of onCreate. 3 00:00:10,120 --> 00:00:16,385 Then on the second new line let's add a comment, // Assign 4 00:00:16,385 --> 00:00:22,940 the Views from the layout file to the corresponding variables. 5 00:00:26,030 --> 00:00:28,780 It's important that we don't assign the views until 6 00:00:28,780 --> 00:00:31,540 after the call to setContentView. 7 00:00:31,540 --> 00:00:36,460 If we try to access a view from our layout before calling setContentView, 8 00:00:36,460 --> 00:00:39,250 it won't exist and we'll get an error. 9 00:00:39,250 --> 00:00:44,347 On the next line, let's start typing factTextView and then an = sign. 10 00:00:45,520 --> 00:00:50,620 And now what we need is a method that will take in the ID that we set on the layout 11 00:00:50,620 --> 00:00:52,250 and return our TextView. 12 00:00:52,250 --> 00:00:57,900 Fortunately, every activity has a method for this, and it's called findViewById. 13 00:00:57,900 --> 00:01:02,304 Let's start typing findViewById after the equals sign. 14 00:01:02,304 --> 00:01:07,054 And notice that as you're typing the auto complete feature is filling it in below. 15 00:01:07,054 --> 00:01:11,156 Android Studio analyzes what we're typing offers suggestions that 16 00:01:11,156 --> 00:01:13,074 match what we've typed so far. 17 00:01:13,074 --> 00:01:17,469 Automatic code completion is awesome, it's super convenient, and it makes it so 18 00:01:17,469 --> 00:01:20,984 that we only need to remember the first few letters of something and 19 00:01:20,984 --> 00:01:23,266 Android Studio will take care of the rest. 20 00:01:23,266 --> 00:01:25,591 Now we can either finish typing this method, or 21 00:01:25,591 --> 00:01:28,340 we can just hit Enter to select it from code completion. 22 00:01:29,610 --> 00:01:33,020 And now we see a hint about the parameter we need to use here. 23 00:01:33,020 --> 00:01:37,150 The findViewById method requires an ID as its parameter. 24 00:01:37,150 --> 00:01:40,260 But rather than just type in the ID from the layout, 25 00:01:40,260 --> 00:01:43,850 we must refer to it through a generated resource class. 26 00:01:43,850 --> 00:01:47,580 When we built our project, Android automatically builds a class for 27 00:01:47,580 --> 00:01:51,420 us called simply R, which stands for resources. 28 00:01:51,420 --> 00:01:54,260 This class contains all the IDs of our files and 29 00:01:54,260 --> 00:01:59,090 the res directory, as well as a ton of other default resource IDs. 30 00:01:59,090 --> 00:02:02,482 If we open the project pane and 31 00:02:02,482 --> 00:02:09,266 switch to the project perspective and then drill into app, 32 00:02:09,266 --> 00:02:14,554 build, generated, source, r, debug, and 33 00:02:14,554 --> 00:02:21,470 then our package name, we can even open the R class. 34 00:02:21,470 --> 00:02:24,400 Though notice at the top there's a warning saying, 35 00:02:24,400 --> 00:02:29,530 files under the build folder are generated and should not be edited. 36 00:02:29,530 --> 00:02:34,435 We're not here to edit anything, it's just nice to know where the R class comes from. 37 00:02:34,435 --> 00:02:37,050 Let's close this and switch back to the Android perspective. 38 00:02:41,140 --> 00:02:43,008 And I'll hide the project pane again. 39 00:02:43,008 --> 00:02:49,650 Back in funfactsactivity.java, let's type a capital R followed by a dot. 40 00:02:49,650 --> 00:02:53,040 Then we need to pick which type of resource we're looking for. 41 00:02:53,040 --> 00:02:57,670 Since we're looking for an ID, let's type id followed by another dot. 42 00:02:59,260 --> 00:03:04,020 And look at that, it's showing us the IDs that we set in our layout. 43 00:03:04,020 --> 00:03:07,441 Let's pick factTextView, and hit Enter to select it, and semicolon. 44 00:03:07,441 --> 00:03:11,582 Now depending on which version of Android you're using, 45 00:03:11,582 --> 00:03:14,199 you might see an error, like I do, or 46 00:03:14,199 --> 00:03:20,690 if you're targeting a newer version of Android, everything will be just fine. 47 00:03:20,690 --> 00:03:21,830 So what happened? 48 00:03:21,830 --> 00:03:25,450 Let's take a look at some code to see what changed in the new version of Android. 49 00:03:26,720 --> 00:03:29,830 Here we've got a Java project where we've mocked out some classes from 50 00:03:29,830 --> 00:03:30,950 the Android API. 51 00:03:32,020 --> 00:03:34,860 We start by creating a fake view class, and 52 00:03:34,860 --> 00:03:39,680 then we create a button and text view classes that extend from our view class. 53 00:03:39,680 --> 00:03:43,920 Then we create an activity class and give it a findViewById method that takes in 54 00:03:43,920 --> 00:03:49,730 an integer and returns a view, just like you'd find in the actual activity class. 55 00:03:49,730 --> 00:03:53,100 Inside this method, we create a local view variable. 56 00:03:53,100 --> 00:03:58,100 And depending on whether the ID is a one or a two, we set it equal to 57 00:03:58,100 --> 00:04:03,670 either a new button or a new text view before returning a view variable. 58 00:04:03,670 --> 00:04:08,230 Finally we've got a main activity class which extends from activity, and inside 59 00:04:08,230 --> 00:04:13,660 that class' constructor we try to set up two variables, a button and a text view. 60 00:04:14,870 --> 00:04:20,270 This is the same error I'm getting back in Android Studio, incompatible types. 61 00:04:20,270 --> 00:04:25,530 It's expecting a button but we're setting it equal to a view to fix this. 62 00:04:25,530 --> 00:04:30,020 Since we know that findViewById with the parameter 63 00:04:30,020 --> 00:04:34,270 of 1 will indeed return a button, we can just cache the returned 64 00:04:34,270 --> 00:04:38,890 value to a button by adding the word Button and parentheses. 65 00:04:41,214 --> 00:04:43,115 And we can do the same thing for 66 00:04:43,115 --> 00:04:46,320 a text view by using Alt Enter to apply a quick fix. 67 00:04:48,080 --> 00:04:52,100 However, in Android Oreo they made a change to the way we 68 00:04:52,100 --> 00:04:56,680 use findViewById that means we no longer need the cast. 69 00:04:56,680 --> 00:04:58,210 So let's undo the casts. 70 00:04:59,734 --> 00:05:03,916 And now let's take a look at how they change the findViewById function. 71 00:05:03,916 --> 00:05:08,101 Up in the findViewById method, instead of returning a view, 72 00:05:08,101 --> 00:05:13,810 we want to use something called generics to let us return multiple types. 73 00:05:13,810 --> 00:05:16,970 Don't worry too much if this part doesn't make sense. 74 00:05:16,970 --> 00:05:22,600 What's important is how it affects our view variables, not how it's implemented. 75 00:05:22,600 --> 00:05:24,790 Let's start by declaring our generic and 76 00:05:24,790 --> 00:05:27,610 what types it's allowed to have inside angle brackets. 77 00:05:31,604 --> 00:05:35,304 Here we're declaring a generic, named T, and 78 00:05:35,304 --> 00:05:40,920 saying it can only be a class with the view class as a parent. 79 00:05:40,920 --> 00:05:48,690 Then instead of returning a view, let's return our new generic type, T. 80 00:05:48,690 --> 00:05:53,550 Finally since we've changed the return type from View to our generic type 81 00:05:53,550 --> 00:05:58,450 that extends View, we need to add a cast to our view variable before we return it. 82 00:06:00,746 --> 00:06:07,275 Perfect, now we no longer need to add a cast to our findViewById calls. 83 00:06:07,275 --> 00:06:10,510 Though since this change is fairly recent, you're likely to encounter 84 00:06:10,510 --> 00:06:15,530 a lot of code that still uses casts with findViewById. 85 00:06:15,530 --> 00:06:18,530 So if you see any code that's still using casts and 86 00:06:18,530 --> 00:06:21,820 it's not giving you issues, feel free to ignore it. 87 00:06:21,820 --> 00:06:24,320 You're just on a newer version of Android than we are.