1 00:00:00,150 --> 00:00:03,290 The last thing we need to is add a menu. 2 00:00:03,290 --> 00:00:05,360 An easy way to do this, 3 00:00:05,360 --> 00:00:08,070 is to copy the code from a project that already has a menu. 4 00:00:09,100 --> 00:00:11,620 Luckily, Android studio creates lots of those. 5 00:00:12,650 --> 00:00:16,563 Let's pick this one, with the three dots in the corner. 6 00:00:26,044 --> 00:00:29,150 We'll want this menumain.xml file for ourselves. 7 00:00:30,400 --> 00:00:33,270 We don't even have a menu folder under our res directory yet. 8 00:00:35,540 --> 00:00:39,153 So I'll just copy the whole directory. 9 00:00:43,533 --> 00:00:47,690 This is the string that will display when we click the menu button. 10 00:00:49,310 --> 00:00:51,430 So let's hit Alt+Enter to create this. 11 00:00:53,150 --> 00:00:54,990 We want it to say clear strokes. 12 00:00:57,280 --> 00:00:59,830 It would also be nice if it had a more descriptive name. 13 00:01:02,060 --> 00:01:07,390 We can go to re-factor, rename, noting that the keyboard shortcut is shift F6, 14 00:01:10,150 --> 00:01:14,690 and then name it action clear strokes. 15 00:01:18,090 --> 00:01:20,460 And we'll do the same thing for the ID. 16 00:01:20,460 --> 00:01:25,337 Except this time we'll use the Shift+F6 shortcut. 17 00:01:30,118 --> 00:01:30,698 All right. 18 00:01:33,397 --> 00:01:37,310 Now we need to add the code and main activity to use that menu. 19 00:01:39,510 --> 00:01:42,803 We can grab that code from the project we just generated as well. 20 00:01:46,343 --> 00:01:47,540 And main activity. 21 00:01:48,660 --> 00:01:53,375 Below onCreate we'll want the onCreateMenuOptions and 22 00:01:53,375 --> 00:01:56,723 onOptionsItemSelected file methods. 23 00:02:01,224 --> 00:02:02,483 And we'll paste them below on pause. 24 00:02:09,504 --> 00:02:14,710 Recall that we changed our action settings to be action clear strokes. 25 00:02:17,080 --> 00:02:21,880 And now we can add the code that we'd like to execute when that button is pressed. 26 00:02:21,880 --> 00:02:27,250 What we'd like to happen is for each of the holes to reset their stroke count 27 00:02:27,250 --> 00:02:32,670 to zero, as well as to not to have any saved data with those strokes either. 28 00:02:33,820 --> 00:02:34,820 So we'll take our editor. 29 00:02:37,300 --> 00:02:42,346 And we'll hit clear to remove all the shared preference values, 30 00:02:42,346 --> 00:02:48,090 and then mEditor.apply to save our changes. 31 00:02:50,120 --> 00:02:54,090 After that, we need to set each of the whole scores to zero. 32 00:02:55,790 --> 00:03:01,400 Instead of using a for loop, using int I equals zero, like I have been doing. 33 00:03:01,400 --> 00:03:03,890 This time I'll use a for each loop to show something different. 34 00:03:05,260 --> 00:03:09,190 So four, we're going to be using holes. 35 00:03:09,190 --> 00:03:16,023 And we're going to call the holes in the loop hole, from the group called m holes. 36 00:03:18,083 --> 00:03:23,004 And for each hole, we wanna set the stroke count to zero. 37 00:03:25,703 --> 00:03:26,283 Okay. 38 00:03:27,883 --> 00:03:29,320 That should do it. 39 00:03:29,320 --> 00:03:30,384 Let's see what happens. 40 00:03:32,304 --> 00:03:37,350 Well, right off the bat, it's pretty clear that we don't have a bar up here. 41 00:03:37,350 --> 00:03:39,070 And we'd like one. 42 00:03:39,070 --> 00:03:41,080 Let's hop over to Google and see what that's all about. 43 00:03:42,170 --> 00:03:44,860 I Googled for list activity, no action bar. 44 00:03:46,010 --> 00:03:47,033 And on the second result, 45 00:03:53,174 --> 00:03:57,780 We learn that only themes after Holo can get action bars. 46 00:03:58,970 --> 00:04:02,290 And our theme, let's double check it against this first, 47 00:04:07,480 --> 00:04:11,300 is AppCompat.Light.DarkActionBar. 48 00:04:11,300 --> 00:04:13,640 Which is exactly what he says might cause us trouble. 49 00:04:15,720 --> 00:04:20,515 So let's try changing it to 50 00:04:20,515 --> 00:04:25,107 Android:Theme.Holo. 51 00:04:32,208 --> 00:04:33,130 All right. 52 00:04:33,130 --> 00:04:33,990 We've got our action bar. 53 00:04:35,250 --> 00:04:39,950 And we've got an awfully dark theme, if you like this theme that's great, 54 00:04:39,950 --> 00:04:44,742 if you don't we can change this to Holo.Light. 55 00:04:44,742 --> 00:04:46,810 Looks good. 56 00:04:46,810 --> 00:04:49,708 So what happens when we clear the strokes? 57 00:04:49,708 --> 00:04:54,193 Nothing, nothing happened. 58 00:04:56,753 --> 00:05:01,460 Three, two, one, zeros, zeros. 59 00:05:05,020 --> 00:05:08,840 What's going on here is when we clear the strokes, 60 00:05:08,840 --> 00:05:12,540 we're clearing them in the data and the data is correct. 61 00:05:12,540 --> 00:05:15,230 But what's being shown to us is not being updated. 62 00:05:15,230 --> 00:05:17,070 We haven't told the list view to update. 63 00:05:18,190 --> 00:05:23,810 By scrolling up and down, we can make it update each of the views, and so 64 00:05:23,810 --> 00:05:29,990 if we clear the strokes and scroll down, and then come back up, it'll be zero. 65 00:05:29,990 --> 00:05:35,230 But it won't be 0 right when we hit Clear Strokes because we haven't added any code 66 00:05:35,230 --> 00:05:35,880 to clear that yet. 67 00:05:39,640 --> 00:05:40,880 This is an easy fix though. 68 00:05:43,710 --> 00:05:49,440 In MainActivity.java, right after we set all the hole stroke counts to 0 69 00:05:49,440 --> 00:05:55,550 All we need to do is call notifyDataSetChanged on our list adaptor. 70 00:05:57,040 --> 00:06:02,020 So, mlistadaptor.notifyDataSetChanged. 71 00:06:02,020 --> 00:06:05,750 And this tells the list adaptor that the data set changed. 72 00:06:05,750 --> 00:06:08,560 And the list adaptor knows that when that happens it needs to update. 73 00:06:10,370 --> 00:06:11,950 So that should handle that bug. 74 00:06:11,950 --> 00:06:12,920 Let's run it and find out. 75 00:06:15,400 --> 00:06:20,590 All right, lets add some scores and then clear them. 76 00:06:20,590 --> 00:06:21,800 Looks good. 77 00:06:21,800 --> 00:06:27,410 And if we leave, and then come back, still zeros. 78 00:06:31,650 --> 00:06:35,210 Let's test a rotation just for good measure. 79 00:06:37,430 --> 00:06:41,450 Looks good. And it looks like we're done with the app. 80 00:06:41,450 --> 00:06:42,680 Great job. 81 00:06:42,680 --> 00:06:43,340 If you're looking for 82 00:06:43,340 --> 00:06:47,070 more things to add to this app, a good place to start would be the app icon. 83 00:06:48,360 --> 00:06:51,170 Congratulations on finishing the Golf Scorecard app. 84 00:06:51,170 --> 00:06:53,390 It was definitely a lot of work. 85 00:06:53,390 --> 00:06:55,650 But being able to put all the pieces together and 86 00:06:55,650 --> 00:06:58,550 create a finished product, is definitely worth it. 87 00:06:58,550 --> 00:07:01,650 You’re now well on your way to becoming an Android developer, and 88 00:07:01,650 --> 00:07:03,980 I look forward to seeing what you choose to make next.