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