This course will be retired on June 1, 2025.
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 we'll finally finish the solitaire app!
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
We've just finished up
our TableauPileView and
0:00
all that's left is to add it to the screen
and make sure we're updating it.
0:03
Starting in main activity, let's create
a property for our TableauPileViews.
0:07
Val tableauPileViews and
0:13
this is gonna be an array
of TableauPileView?
0:18
And we're going to set to = to
on an arrayOfNulls of size 7.
0:25
And again,
we still don't need this kotlin dot part.
0:31
Next, inside our second linear layout,
Let's add a loop from zero to six.
0:34
So four i in zero to 6 and
inside this loop
0:43
let's populate our TableauPileViews.
0:48
TableauPileViews at index i is = to a new
0:56
tableauPileView where we
pass in the index of i.
1:00
Then let's call the lparams function and
1:06
pass cardWidth for the width and
matchParent for the height.
1:10
TableauPileView can be pretty tall.
1:16
Next, since we're making the tableau piles
have a height of match parent, let's
1:20
give the linear layout there are part
of the height of match parent as well.
1:24
So let's add an Lparams function to this,
and
1:30
then let's set the height
= to matchParent.
1:34
Lastly, let's add brackets to
this last lparams function and
1:38
inside let's set topMargin=cardHeight/2.
1:45
This way, we'll have some space between
our first row and our tableau piles.
1:51
Then let's add our tableauPileViews
to the update function.
1:56
So TableauPileViews.forEach, and it,
2:01
which we assert is not null dot update and
2:05
now let's finally play some solitaire.
2:09
Pretty cool, right?
2:20
Wait, what was that?
2:25
You guys saw that right?
2:29
It looks like we've got a bug.
2:31
It let me click a face down card and
a tableau pile.
2:33
And it moved it over here.
2:38
To fix this,
let's start in tableau pile view.
2:40
Inside the On Click listener.
2:45
Then, let's use Cmd or
Ctrl+B to follow the trail and
2:48
in to the onTableauTap function.
2:52
Then let's go deeper yet
2:55
by jumping into the game model
version of the onTableauTab function.
2:56
In here, we can see that we
aren't doing any kind of handling
3:01
about whether a card is face up or
not and we really should be.
3:05
So inside this first if statement, after
we've made sure that there are cards.
3:11
Let's add another if statement to make
sure that the tapped card is face up.
3:17
If tableauPile.cards
3:22
At cardIndex is faceUp.
3:31
And then let's only try to
play a card if we can see it.
3:37
All right.
3:42
That should take care of that bug.
3:42
But before we run the app again,
let's quickly make it so
3:44
that we can restart the game
without closing it.
3:47
Over in main activity, let's add some
space below the update method and
3:51
use Ctrl+O to override
the onCreateOptionsMenu method.
3:57
Then, since this will be passing in a
menu, let's get rid of this question mark.
4:04
And inside this method let's add a start
over menu item by calling the add
4:09
function on our menu parameter, And
then passing in Start Over as a string.
4:15
Finally, let's just return true to
tell Android to show this menu.
4:27
Now we've added the menu item, we need to
handle what happens when it gets selected.
4:33
Below this function,
4:38
let's use Ctrl+O again to override
the on options item selected method.
4:40
Then let's remove the question mark for
menu item and
4:46
return true to let Android know
that we've got this covered.
4:49
Lastly, let's add some space at
the top of this function, And
4:56
then we just need to reset the gameModel,
5:02
gameModel.resetGame and
then update the views.
5:05
Update.
5:10
Also thanks to game model being
a singleton we never really
5:11
ended up needing the game model as
a parameter to the update function.
5:15
So we should be safe to get rid of it.
5:19
Which means our game view interface
inside the no_android folder can be a lot
5:22
shorter.
5:27
All right, let's get back to
the action and run the app.
5:32
And now we can click as many
facedown cards as we want and
5:37
they'll all stay right there.
5:41
Also we've now got this new start
over option which lets us deal
5:45
out a new game if we get stuck or
if we just want a better start.
5:50
We've been working on this app for
a while now.
5:58
We started out with the very
basics of Kotlin and
6:00
we wound our way all the way
from learning about properties
6:03
to learning about how we can create
an Android app without a layout folder.
6:05
Anko is one of the newest and coolest
ways for us to make apps in Android.
6:09
And while Anko's still
not had its 1.0 release,
6:14
I think you'll agree that it's a pretty
neat way to make an Android app.
6:16
I hope you had just as much fun taking
this course as I did creating it.
6:20
And I can't wait to see all the cool
things you're going to make
6:24
using Kotlin and Anko.
6:27
Until next time
6:29
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