1 00:00:00,000 --> 00:00:04,977 [MUSIC] 2 00:00:04,977 --> 00:00:08,398 Now that we're comfortable using the design editor to make changes to our 3 00:00:08,398 --> 00:00:10,890 layouts, it's time to write some code. 4 00:00:10,890 --> 00:00:13,470 Let's start by taking a look at some of the code that was 5 00:00:13,470 --> 00:00:15,210 automatically generated for our project. 6 00:00:16,400 --> 00:00:19,820 Using the Android perspective makes our code easy to find. 7 00:00:19,820 --> 00:00:22,190 It's right here in the Java folder. 8 00:00:22,190 --> 00:00:26,759 But if you happen to be in the project perspective, 9 00:00:26,759 --> 00:00:33,690 remember that our Java code can be found at app > source > main and then java. 10 00:00:33,690 --> 00:00:35,760 It's right above our resources folder. 11 00:00:35,760 --> 00:00:37,745 Let's switch back to the Android perspective. 12 00:00:39,962 --> 00:00:41,900 And then let's look inside the java folder. 13 00:00:43,430 --> 00:00:47,401 Now we can see the package name we choose when we create our project. 14 00:00:47,401 --> 00:00:50,769 This is where all of the code will go by default. 15 00:00:50,769 --> 00:00:56,510 Inside this package we find one file FunFactActivity.java. 16 00:00:56,510 --> 00:00:58,460 It's not showing the .java but it's there. 17 00:00:59,630 --> 00:01:03,340 And if we double click on it, it opens over here in the editor. 18 00:01:04,410 --> 00:01:06,670 Now we can close the rest of these files. 19 00:01:06,670 --> 00:01:12,795 Let's right click on FunFactsActivity.java up here and choose Close Others. 20 00:01:12,795 --> 00:01:14,800 And we'll go ahead and hide the project view for now. 21 00:01:15,940 --> 00:01:20,790 In Android, an activity represents a screen that users can interact with. 22 00:01:20,790 --> 00:01:23,230 We'll learn much more about activities later. 23 00:01:23,230 --> 00:01:26,180 But for now, let's focus on this onCreate method. 24 00:01:27,200 --> 00:01:31,390 The onCreate method is called when our activity is first created and 25 00:01:31,390 --> 00:01:36,460 since our app only has this one activity this onCreate method 26 00:01:36,460 --> 00:01:39,160 will be called when our app is first started. 27 00:01:39,160 --> 00:01:42,520 You might have noticed that this method also has one parameter, 28 00:01:42,520 --> 00:01:45,600 a bundle names savedInstanceStae. 29 00:01:45,600 --> 00:01:49,240 We won't be using this variable in our app, so let's ignore it for now. 30 00:01:49,240 --> 00:01:54,772 Inside onCreate, the most important line is the call to setContentView. 31 00:01:54,772 --> 00:01:59,603 This method tells the activity which layout file to use for the screen. 32 00:01:59,603 --> 00:02:04,422 This R.layout.activity_fun_facts parameter is an id which points 33 00:02:04,422 --> 00:02:09,210 to the activity_fun_facts XML file in the layout directory. 34 00:02:09,210 --> 00:02:12,745 This is where the layout we've been working on gets attached to our activity. 35 00:02:12,745 --> 00:02:15,900 Okay, time to make the magic happen. 36 00:02:15,900 --> 00:02:20,470 The first thing we need to do is declare two fields or member variables. 37 00:02:20,470 --> 00:02:24,460 One for our fun fact text view and one for our button. 38 00:02:24,460 --> 00:02:25,820 A field is the name for 39 00:02:25,820 --> 00:02:31,350 a variable that is inside the class but outside all of the methods. 40 00:02:31,350 --> 00:02:32,080 Using fields for 41 00:02:32,080 --> 00:02:36,940 our views means that we'll be able to access them from any method in our class. 42 00:02:36,940 --> 00:02:40,680 This is especially useful because there is often more than one method 43 00:02:40,680 --> 00:02:43,050 that needs access to our views. 44 00:02:43,050 --> 00:02:46,440 Let's start by adding a line at the top of the class. 45 00:02:46,440 --> 00:02:49,710 Then let's add a comment to help explain what we're doing. 46 00:02:49,710 --> 00:02:53,140 Remember, it's two forward slashes for a single line comment. 47 00:02:53,140 --> 00:02:59,130 Let's type // space, and then, say, declare our view variables. 48 00:03:00,650 --> 00:03:05,694 Then on new line, let's declare our fun fact 49 00:03:05,694 --> 00:03:10,882 text view as private TextView FactTextView. 50 00:03:13,499 --> 00:03:16,720 And if TextView is in red, that means we haven't imported it yet. 51 00:03:17,880 --> 00:03:21,820 So put your cursor on it and hit Alt + Enter to import it. 52 00:03:23,320 --> 00:03:27,620 Also, note that we won't be using the m prefix for our fields. 53 00:03:27,620 --> 00:03:30,090 According to the Android source style guide, 54 00:03:30,090 --> 00:03:33,660 all fields should start with a lower case m, like this. 55 00:03:34,790 --> 00:03:37,580 However, most code isn't written that way. 56 00:03:37,580 --> 00:03:40,960 So unless you plan on contributing to Android's source code, 57 00:03:40,960 --> 00:03:44,340 feel free to drop the m prefix as we'll be doing here. 58 00:03:44,340 --> 00:03:46,750 If you'd like to read more about these style guidelines, 59 00:03:46,750 --> 00:03:49,270 check out the link in the teacher's notes below. 60 00:03:49,270 --> 00:03:51,210 Now we need to declare our button. 61 00:03:51,210 --> 00:03:53,700 Let's add a new line below our text view and 62 00:03:53,700 --> 00:03:57,360 type private and then start typing Button. 63 00:03:58,950 --> 00:04:03,200 Notice that Android Studio is trying to guess what we're typing. 64 00:04:03,200 --> 00:04:05,530 Once the top choice is Button, hit Enter or 65 00:04:05,530 --> 00:04:08,420 Tab to let Android Studio autocomplete that for us. 66 00:04:09,480 --> 00:04:11,740 It even adds the import statement. 67 00:04:11,740 --> 00:04:17,170 Then, when we type a lowercase b, Android Studio suggests a name of Button. 68 00:04:17,170 --> 00:04:18,500 Cool, right? 69 00:04:18,500 --> 00:04:20,840 But we'd rather have a more descriptive name. 70 00:04:20,840 --> 00:04:23,207 Let's name it showFactButton. 71 00:04:25,663 --> 00:04:29,660 All right, but why did these names change to grey like this? 72 00:04:29,660 --> 00:04:31,810 If we hover over one of our fields, 73 00:04:31,810 --> 00:04:36,520 we get a quick tip saying private field is never used. 74 00:04:36,520 --> 00:04:40,120 The grey coloring will go away once we use these variables. 75 00:04:40,120 --> 00:04:43,340 Another thing I want to point out are the import statements. 76 00:04:43,340 --> 00:04:45,830 We can view them hitting the little plus button up here. 77 00:04:47,260 --> 00:04:50,790 By default, our class only has a couple of imports. 78 00:04:50,790 --> 00:04:55,130 But every time we use the new class, like TextView or Button, 79 00:04:55,130 --> 00:05:00,000 Android Studio will try to automatically add the corresponding import statement. 80 00:05:00,000 --> 00:05:02,469 If you use a class that isn't yet imported, 81 00:05:02,469 --> 00:05:04,688 let's say I delete the Button import. 82 00:05:06,658 --> 00:05:09,140 Then you'll have something that looks like this. 83 00:05:09,140 --> 00:05:13,110 To fix it, just place your cursor on the missing class and 84 00:05:13,110 --> 00:05:14,420 hit Alt + Enter to import it. 85 00:05:15,930 --> 00:05:17,000 Let's take a short break and 86 00:05:17,000 --> 00:05:19,980 then we'll see how to assign our views to these new variables.