Accessing Views in Code: Declaring Variables5:20 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 Java 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:19 But if you happen 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:33 Let's switch back to the Android perspective. 0:35 And then let's look inside the java folder. 0:39 Now we can see the package name we choose when we create our project. 0:43 This is where all of the code will go by default. 0:47 Inside this package we find one file FunFactActivity.java. 0:50 It's not showing the .java but it's there. 0:56 And if we double click on it, it opens over here in the editor. 0:59 Now we can close the rest of these files. 1:04 Let's right click on FunFactsActivity.java up here and choose Close Others. 1:06 And we'll go ahead and hide the project view for now. 1:12 In Android, an activity represents a screen that users can interact with. 1:15 We'll learn much more about activities later. 1:20 But for now, let's focus on this onCreate method. 1:23 The onCreate method is called when our activity is first created and 1:27 since our app only has this one activity this onCreate method 1:31 will be called when our app is first started. 1:36 You might have noticed that this method also has one parameter, 1:39 a bundle names savedInstanceStae. 1:42 We won't be using this variable in our app, so let's ignore it for now. 1:45 Inside onCreate, the most important line is the call to setContentView. 1:49 This method tells the activity which layout file to use for the screen. 1:54 This R.layout.activity_fun_facts parameter is an id which points 1:59 to the activity_fun_facts XML file in the layout directory. 2:04 This is where the layout we've been working on gets attached to our activity. 2:09 Okay, time to make the magic happen. 2:12 The first thing we need to do is declare two fields or member variables. 2:15 One for our fun fact text view and one for our button. 2:20 A field is the name for 2:24 a variable that is inside the class but outside all of the methods. 2:25 Using fields for 2:31 our views means that we'll be able to access them from any method in our class. 2:32 This is especially useful because there is often more than one method 2:36 that needs access to our views. 2:40 Let's start by adding a line at the top of the class. 2:43 Then let's add a comment to help explain what we're doing. 2:46 Remember, it's two forward slashes for a single line comment. 2:49 Let's type // space, and then, say, declare our view variables. 2:53 Then on new line, let's declare our fun fact 3:00 text view as private TextView FactTextView. 3:05 And if TextView is in red, that means we haven't imported it yet. 3:13 So put your cursor on it and hit Alt + Enter to import it. 3:17 Also, note that we won't be using the m prefix for our fields. 3:23 According to the Android source style guide, 3:27 all fields should start with a lower case m, like this. 3:30 However, most code isn't written that way. 3:34 So unless you plan on contributing to Android's source code, 3:37 feel free to drop the m prefix as we'll be doing here. 3:40 If you'd like to read more about these style guidelines, 3:44 check out the link in the teacher's notes below. 3:46 Now we need to declare our button. 3:49 Let's add a new line below our text view and 3:51 type private and then start typing Button. 3:53 Notice that Android Studio is trying to guess what we're typing. 3:58 Once the top choice is Button, hit Enter or 4:03 Tab to let Android Studio autocomplete that for us. 4:05 It even adds the import statement. 4:09 Then, when we type a lowercase b, Android Studio suggests a name of Button. 4:11 Cool, right? 4:17 But we'd rather have a more descriptive name. 4:18 Let's name it showFactButton. 4:20 All right, but why did these names change to grey like this? 4:25 If we hover over one of our fields, 4:29 we get a quick tip saying private field is never used. 4:31 The grey coloring will go away once we use these variables. 4:36 Another thing I want to point out are the import statements. 4:40 We can view them hitting the little plus button up here. 4:43 By default, our class only has a couple of imports. 4:47 But every time we use the new class, like TextView or Button, 4:50 Android Studio will try to automatically add the corresponding import statement. 4:55 If you use a class that isn't yet imported, 5:00 let's say I delete the Button import. 5:02 Then you'll have something that looks like this. 5:06 To fix it, just place your cursor on the missing class and 5:09 hit Alt + Enter to import it. 5:13 Let's take a short break and 5:15 then we'll see how to assign our views to these new variables. 5:17
You need to sign up for Treehouse in order to download course files.Sign up