Accessing Views in Code: Declaring Variables5:41 with Ben Deitch
Now that we've covered a bit of the generated code, it's time to write our own! In this video we'll write Kotlin code in our FunFactsActivity class to declare our TextView and Button.
[MUSIC] 0:00 Now that we're comfortable using the design editor to make changes to our 0:04 layouts, it's time to write some code. 0:08 Let's start by taking a look at some of the code that was 0:10 automatically generated for our project. 0:13 Using the Android perspective makes our code easy to find, 0:16 it's right here in the java folder. 0:20 But if you happened to be in the project perspective. 0:22 Remember that our Java code can be found at app > source > main > and then Java. 0:26 It's right above our resources folder. 0:32 Let's switch back to the Android perspective and 0:35 take a look inside the java folder. 0:38 Now we can see the package name we chose when we created our project. 0:41 This is where all of our code will go by default. 0:45 Inside this package, we'll find one file, FunFactsActivity. 0:48 And if we double click it, it opens up over here in the code editor. 0:53 Now we can close the rest of these files. 0:58 Let's right click on FunFactsActivity up here and choose Close Others. 1:00 And I'll also close the project pane. 1:07 In Android, an activity represents a screen that users can interact with. 1:10 We'll learn much more about activities later but for 1:15 now, let's focus on this onCreate method. 1:17 The onCreate method is called when our activity is first created. 1:21 And since our app only has one activity, 1:24 this onCreate method will be called when our app is first started. 1:27 You might have noticed that this method also has one parameter, 1:31 a bundled variable called savedInstanceState. 1:34 We won't be using this variable in our app, so let's ignore it for now. 1:38 Inside onCreate, the most important line is the call to setContentView. 1:42 This method tells the activity which layout file we want to use for the screen. 1:47 This R.layout.activity_fun_facts parameter, is an ID 1:51 which points to the activity_fun_facts XML file and the layout directory. 1:56 This is where the layout we've been working on gets attached to our activity. 2:01 Okay, time to make the magic happen. 2:06 The first thing we need to do is declare two properties or member variables. 2:08 One for our FunFact text view and one for our button. 2:13 A property is the name for a variable that is inside the class but 2:16 outside all of the methods. 2:21 Using properties for 2:23 our views means that we'll be able to access them from any method in our class. 2:24 This is especially useful because there's often more than one method that needs 2:29 access to our views. 2:33 Let's start by adding a line at the top of the class. 2:34 Then let's add a comment to help explain what we're doing. 2:39 Remember it's two forward slashes for a single line comment. 2:42 Let's type slash, slash space and then type Declare our View variables. 2:46 Then on a new line let's declare our fun fact text view as private var 2:54 factTextView, Then since, 3:00 we're using colon, we also need to set it equal to something. 3:05 But we won't have access to our text view and until the onCreate method. 3:10 So for now, let's just set it equal to no. 3:15 Let's have a colon and then for the data type, let's use TextView but 3:18 add a question mark to the end to let colon know it might be null. 3:23 Then let's set it equal to null and there we go. 3:29 Also, if TextView is in red, that just means it hasn't been imported yet. 3:33 Hit Alt+Enter to import it. 3:38 One more thing, note that we won't be using the m prefix for our properties. 3:40 Sometimes, when you see Android code, they might declare their variables like this. 3:45 And according to the Android source style guide, all fields or 3:51 properties should start with a lower case m. 3:54 However, most code isn't written that way. 3:58 So unless you plan on contributing to Android source code, 4:01 feel free to drop the m prefix as we'll be doing here. 4:04 If you'd like to read more about these style guidelines, 4:08 check out the link in the teacher's notes below. 4:11 Now we need to declare our button. 4:13 Let's add a new line in our TextView and type private var showFactButton. 4:15 Then, let's add a colon and for the type, start typing Button. 4:22 Notice that Android Studio is trying to guess what we're typing. 4:28 Once the top choice is button, hit Enter or 4:32 Tab to let Android Studio autocomplete that for us. 4:35 It even adds the import statement. 4:39 Then, to finish it up, let's add a question mark and equals null. 4:41 All right, but why do these names change to gray like this? 4:47 If we hover over one of our properties, 4:52 we get a quick tip saying that our Property is never used. 4:54 The gray coloring will go away once we use these variables. 4:57 Another thing I want to point out are the import statements, 5:01 we can view them by hitting the little plus button up here. 5:04 By default, our class only has a couple of imports, 5:08 but every time we use a new class like TextView or button. 5:11 Android Studio will try to automatically add the corresponding import statement, 5:15 if you use a class that isn't important yet. 5:20 Let say I delete the button import. 5:23 Then you'll have something it'll look like this. 5:27 To fix it, just place your cursor in the missing class and 5:30 hit Alt+Enter to import it. 5:34 Let's take a short break and 5:36 then we'll see how to assign our views to these new variables. 5:38
You need to sign up for Treehouse in order to download course files.Sign up