Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video I find a way to get back the ActionBar, implement the Menu, and add functionality to the 'Clear Strokes' MenuItem.
-
0:00
The last thing we need to is add a menu.
-
0:03
An easy way to do this,
-
0:05
is to copy the code from a project that already has a menu.
-
0:09
Luckily, Android studio creates lots of those.
-
0:12
Let's pick this one, with the three dots in the corner.
-
0:26
We'll want this menumain.xml file for ourselves.
-
0:30
We don't even have a menu folder under our res directory yet.
-
0:35
So I'll just copy the whole directory.
-
0:43
This is the string that will display when we click the menu button.
-
0:49
So let's hit Alt+Enter to create this.
-
0:53
We want it to say clear strokes.
-
0:57
It would also be nice if it had a more descriptive name.
-
1:02
We can go to re-factor, rename, noting that the keyboard shortcut is shift F6,
-
1:10
and then name it action clear strokes.
-
1:18
And we'll do the same thing for the ID.
-
1:20
Except this time we'll use the Shift+F6 shortcut.
-
1:30
All right.
-
1:33
Now we need to add the code and main activity to use that menu.
-
1:39
We can grab that code from the project we just generated as well.
-
1:46
And main activity.
-
1:48
Below onCreate we'll want the onCreateMenuOptions and
-
1:53
onOptionsItemSelected file methods.
-
2:01
And we'll paste them below on pause.
-
2:09
Recall that we changed our action settings to be action clear strokes.
-
2:17
And now we can add the code that we'd like to execute when that button is pressed.
-
2:21
What we'd like to happen is for each of the holes to reset their stroke count
-
2:27
to zero, as well as to not to have any saved data with those strokes either.
-
2:33
So we'll take our editor.
-
2:37
And we'll hit clear to remove all the shared preference values,
-
2:42
and then mEditor.apply to save our changes.
-
2:50
After that, we need to set each of the whole scores to zero.
-
2:55
Instead of using a for loop, using int I equals zero, like I have been doing.
-
3:01
This time I'll use a for each loop to show something different.
-
3:05
So four, we're going to be using holes.
-
3:09
And we're going to call the holes in the loop hole, from the group called m holes.
-
3:18
And for each hole, we wanna set the stroke count to zero.
-
3:25
Okay.
-
3:27
That should do it.
-
3:29
Let's see what happens.
-
3:32
Well, right off the bat, it's pretty clear that we don't have a bar up here.
-
3:37
And we'd like one.
-
3:39
Let's hop over to Google and see what that's all about.
-
3:42
I Googled for list activity, no action bar.
-
3:46
And on the second result,
-
3:53
We learn that only themes after Holo can get action bars.
-
3:58
And our theme, let's double check it against this first,
-
4:07
is AppCompat.Light.DarkActionBar.
-
4:11
Which is exactly what he says might cause us trouble.
-
4:15
So let's try changing it to
-
4:20
Android:Theme.Holo.
-
4:32
All right.
-
4:33
We've got our action bar.
-
4:35
And we've got an awfully dark theme, if you like this theme that's great,
-
4:39
if you don't we can change this to Holo.Light.
-
4:44
Looks good.
-
4:46
So what happens when we clear the strokes?
-
4:49
Nothing, nothing happened.
-
4:56
Three, two, one, zeros, zeros.
-
5:05
What's going on here is when we clear the strokes,
-
5:08
we're clearing them in the data and the data is correct.
-
5:12
But what's being shown to us is not being updated.
-
5:15
We haven't told the list view to update.
-
5:18
By scrolling up and down, we can make it update each of the views, and so
-
5:23
if we clear the strokes and scroll down, and then come back up, it'll be zero.
-
5:29
But it won't be 0 right when we hit Clear Strokes because we haven't added any code
-
5:35
to clear that yet.
-
5:39
This is an easy fix though.
-
5:43
In MainActivity.java, right after we set all the hole stroke counts to 0
-
5:49
All we need to do is call notifyDataSetChanged on our list adaptor.
-
5:57
So, mlistadaptor.notifyDataSetChanged.
-
6:02
And this tells the list adaptor that the data set changed.
-
6:05
And the list adaptor knows that when that happens it needs to update.
-
6:10
So that should handle that bug.
-
6:11
Let's run it and find out.
-
6:15
All right, lets add some scores and then clear them.
-
6:20
Looks good.
-
6:21
And if we leave, and then come back, still zeros.
-
6:31
Let's test a rotation just for good measure.
-
6:37
Looks good. And it looks like we're done with the app.
-
6:41
Great job.
-
6:42
If you're looking for
-
6:43
more things to add to this app, a good place to start would be the app icon.
-
6:48
Congratulations on finishing the Golf Scorecard app.
-
6:51
It was definitely a lot of work.
-
6:53
But being able to put all the pieces together and
-
6:55
create a finished product, is definitely worth it.
-
6:58
You’re now well on your way to becoming an Android developer, and
-
7:01
I look forward to seeing what you choose to make next.
You need to sign up for Treehouse in order to download course files.
Sign up