Accessing Views in Code: Assigning Variables6:35 with Ben Deitch
Now that we've declared our variables, it's time to start using them! In this video we'll see how we can assign these variables to the corresponded Views from our layout.
Awesome, now it's time to assign values to our new view variables. 0:00 Let's start by adding four new lines to the bottom of onCreate. 0:04 Then on the second new line let's add a comment// 0:11 Assign the Views from the lab file to the layout 0:16 file to the corresponding variables. 0:21 It's important that we don't assign the views until 0:27 after the call to setContentView. 0:29 If we try to access a view from our layout before calling setContentView 0:32 it won't exist and we'll get an error. 0:37 On the next line let's start by typing factTextView and then an equals sign. 0:40 And now we need a method that will take in the ID that we set in the layout and 0:46 return our TextView. 0:50 Fortunately, every activity has a method for this and it's called findViewById. 0:52 Let's start by typing findViewById after the equals sign. 0:57 And notice that as your typing, 1:03 the autocomplete feature is filling it in below. 1:05 Android Studio analyzes what we're typing and 1:08 offers suggestions that match what we've typed so far. 1:10 Automatic code completion is awesome. 1:14 It's super convenient and it makes it so we only need to remember the first few 1:17 letters of something and Android Studio will take care of the rest. 1:21 Now we can either finish typing this method or 1:26 we can just hit Enter to select it from code completion. 1:28 And now we see a hint about the parameter we need to use here. 1:33 The findViewById method requires an ID as it's parameter. 1:36 But rather than just type in the ID from the layout, 1:41 we must refer to it through a generated resource class. 1:44 When we build our project, Android automatically builds a class for 1:48 us called simply R, which stands for resources. 1:52 This class contains all of the IDs of our files in the res directory, 1:58 as well as a ton of other default resource ID's. 2:02 If we open the project pane and switch to the project perspective, and 2:06 then drill into app, build, generated, 2:12 source, r, debug, and 2:18 then our package name, we can even open the r class. 2:22 You'll notice at the top there's a warning saying files 2:28 under the build folder are generated and should not be edited. 2:31 We're not in here to edit anything. 2:36 It's just nice to know where the R class comes from. 2:37 Let's close this and switch back to the Android perspective. 2:41 And I'll close the project pane as well. 2:48 Back in FunFactsActivity.kt, 2:51 let's type a capital R followed by a dot. 2:55 Then we need to pick which type of resource we're looking for. 2:59 Since we're looking for an ID, let's type ID followed by another dot. 3:03 Then let's start typing the ID from our layout, fact, text view. 3:10 And there it is, so hit Enter to select it. 3:15 Now, depending on which version of Android you're using, 3:19 you might see an error like I do or 3:22 if you're targeting a newer version of Android, everything will be just fine. 3:25 So what happened? 3:29 Let's take a look at some code to see what changed in the new version of Android. 3:30 Here we've got a column project where we mocked out 3:35 some classes from the Android API. 3:38 We start with a view class and then we create a button, and 3:41 text view classes that extend from our view class. 3:45 Then we create an activity class and give it a findViewById method that takes in 3:49 an integer and returns a view, just like you'd find in the real activity class. 3:53 Inside this method we create a local view variable and depending on whether the ID 4:00 is a one or a two, we set it equal to either a new Button or 4:05 a new TextView, before returning our view variable. 4:10 Finally, we got a MainActivity class which extends from Activity. 4:14 And inside that class's constructor we tried to set up two variables, 4:19 a button, and a text view. 4:23 This is the same error I'm getting back in Android Studio. 4:26 Type mismatch, it's expecting a button, but we're setting it equal to a view. 4:30 To fix this, since we know that findViewById with a parameter of 1 will 4:35 indeed return a button, 4:41 we can just cast the returned value to a button by adding as Button. 4:43 And we can do the same thing for our TextView by hitting Alt Enter. 4:50 However, in Android Oreo, they made a change to the way we use 4:57 findViewById that means we no longer need the cast. 5:01 So let's undo the casts, and 5:06 then see what's been changed in findViewById. 5:11 Up in thefFindViewById method, instead of returning a view we 5:14 want to use something called generics to let us return multiple types. 5:18 Don't worry too much if this part doesn't make sense. 5:23 What's important is how it effects our view variables, not how it's implemented. 5:26 Let's start by declaring our generic type and 5:32 what types it's allowed to have inside angle brackets before the function name. 5:34 Here we're declaring a generic type named t, and 5:43 we're saying it can only be classes that have view as a parent. 5:47 Then, instead of returning a view, let's return our new generic type, T. 5:53 Finally, since we've changed the return type, 6:00 we need to add a cast to our view variable before we return it, as T. 6:03 Perfect, now we no longer need to add a cast to our findViewById calls. 6:09 But since this change is fairly recent, 6:17 you're likely to encounter a lot of code that still uses casts with findViewById. 6:20 So if you see any code that's still using casts and 6:25 it's not giving you any issues, feel free to ignore it. 6:28 You're just on a newer version of Android than we are. 6:31
You need to sign up for Treehouse in order to download course files.Sign up