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