1 00:00:00,450 --> 00:00:03,430 We started with our layout where we created our button. 2 00:00:03,430 --> 00:00:06,280 Then we made a variable for it in our activity class, and 3 00:00:06,280 --> 00:00:09,020 we set that variable in the last code challenge. 4 00:00:09,020 --> 00:00:11,630 But there's still one more step to make our button work. 5 00:00:12,890 --> 00:00:15,380 I'll start by adding the code from the code challenge. 6 00:00:17,890 --> 00:00:22,536 I'll type showF and then hit enter or tab to auto complete. 7 00:00:22,536 --> 00:00:28,020 Then I'll set it = findViewById(R.id.showFactButton) 8 00:00:28,020 --> 00:00:34,537 hit ALT+Enter to add the cast and it looks like I need to move that around and 9 00:00:34,537 --> 00:00:40,560 there we go, ALT+Enter to add the cast and finish with a semicolon. 10 00:00:40,560 --> 00:00:43,080 Now, it's time to make our button do something. 11 00:00:43,080 --> 00:00:46,350 On a new line, type showFactButton and 12 00:00:46,350 --> 00:00:48,980 remember that we can use enter or tab to auto complete. 13 00:00:50,310 --> 00:00:55,710 Now, if we type a dot, we get a new menu for auto complete suggestions. 14 00:00:55,710 --> 00:00:59,020 These are all methods available to a button object. 15 00:01:00,130 --> 00:01:04,570 As we start typing, methods that don't match will be filtered out. 16 00:01:04,570 --> 00:01:05,520 Let's type setOn. 17 00:01:07,070 --> 00:01:11,660 We see the setOn click listener method near the top of our list. 18 00:01:11,660 --> 00:01:14,326 That's also the method we're looking for, so let's select it. 19 00:01:17,016 --> 00:01:20,500 An OnClickListener is exactly what it sounds like. 20 00:01:20,500 --> 00:01:24,340 It's something that listens for the button to be clicked, or tapped. 21 00:01:24,340 --> 00:01:27,070 Clicked and tapped mean the same thing when we're talking about a touch screen. 22 00:01:28,080 --> 00:01:30,150 When the listener detects a click, 23 00:01:30,150 --> 00:01:32,820 it then runs some code that we've given it ahead of time. 24 00:01:33,932 --> 00:01:37,530 A setOnClickListener method is expecting a parameter 25 00:01:37,530 --> 00:01:41,690 that is another new data type on ClickListener. 26 00:01:41,690 --> 00:01:43,880 That's what this call out is telling us. 27 00:01:43,880 --> 00:01:49,300 if you lose this call out somehow, simply hover over that error and 28 00:01:49,300 --> 00:01:53,930 we can see the inside of these parenthesis is supposed to be an on ClickListener. 29 00:01:53,930 --> 00:01:58,670 The listener parameter we provide here will be connected to our showFact button. 30 00:01:58,670 --> 00:02:02,860 It doesn't exist yet, so let's add a line above this one and 31 00:02:02,860 --> 00:02:05,660 start typing OnClickListener with a capital O. 32 00:02:07,270 --> 00:02:10,420 Notice that there's more than one type of OnClickListener. 33 00:02:10,420 --> 00:02:16,090 This first one is for a view and the second one is for a dialogue interface. 34 00:02:16,090 --> 00:02:19,260 But how do we know which one of these we should choose? 35 00:02:19,260 --> 00:02:22,500 There's actually a couple different ways we can get more information about 36 00:02:22,500 --> 00:02:24,880 objects and methods like this one. 37 00:02:24,880 --> 00:02:28,693 One way is to Google the class name, button, and 38 00:02:28,693 --> 00:02:33,446 then the method we're looking for, setOnClickListener. 39 00:02:36,109 --> 00:02:39,230 This first link is to the official Android Documentation. 40 00:02:40,520 --> 00:02:43,300 Let's open it, and then use Cmd or 41 00:02:43,300 --> 00:02:46,980 Ctrl + F to find where it mentions setOnClickListener. 42 00:02:48,030 --> 00:02:51,120 Right at the top, there's some sample code for a button, and 43 00:02:51,120 --> 00:02:54,509 it looks like it's using the OnClickListener from the View class. 44 00:02:55,590 --> 00:02:56,930 Back in Android Studio, 45 00:02:56,930 --> 00:03:00,530 we can use a shortcut to get at the documentation as well. 46 00:03:00,530 --> 00:03:05,453 Let's click on setOnClickListener, and then use F1 for 47 00:03:05,453 --> 00:03:08,080 Mac, or Ctrl+Q for Windows. 48 00:03:08,080 --> 00:03:10,680 This brings up the quick documentation dialogue, and 49 00:03:10,680 --> 00:03:14,900 again, we can see that we're looking for the OnClickListener from the view class. 50 00:03:16,100 --> 00:03:20,640 Now that we know which OnClickListener to use, let's go back up here and 51 00:03:20,640 --> 00:03:23,620 keep typing to bring back auto-complete. 52 00:03:23,620 --> 00:03:27,270 Then let's select the View.OnClickListener and 53 00:03:27,270 --> 00:03:31,940 notice the Android studio makes it very clear which OnClickListener we chose. 54 00:03:31,940 --> 00:03:35,060 This way we can avoid any confusion between the different 55 00:03:35,060 --> 00:03:37,080 On Click Listener classes. 56 00:03:37,080 --> 00:03:41,230 Next, we need to name our listener and then set it equal to something. 57 00:03:41,230 --> 00:03:44,540 Let's name it something descriptive, like listener. 58 00:03:44,540 --> 00:03:49,960 Then type space equals space, new, another space and 59 00:03:49,960 --> 00:03:54,720 then type on click listener with a capital o and hit enter to auto complete. 60 00:03:55,970 --> 00:04:00,160 Whoa, it just dropped in a lot of code for us. 61 00:04:00,160 --> 00:04:04,200 This is all the code that's required to create an on click listener. 62 00:04:04,200 --> 00:04:07,230 But we still need to end our statement with a semicolon, so 63 00:04:07,230 --> 00:04:10,560 before we forget let's add a semicolon after this last bracket. 64 00:04:11,780 --> 00:04:15,460 This may seem like a lot, but that's why we have auto complete. 65 00:04:15,460 --> 00:04:20,310 It's a lot easier to use auto complete than it is to remember all of that. 66 00:04:20,310 --> 00:04:25,330 Before we move on, let's quickly copy our listener variable and 67 00:04:25,330 --> 00:04:31,050 paste it down here as the parameter for setOnClickListener. 68 00:04:31,050 --> 00:04:35,000 And now that there's no error let's take a short break before we finish our listener.