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 finish learning how the app works!
Project Files
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 got a pizza object and
a topping object.
0:00
And when the app is created
we use the toppings list
0:03
to generate our topping bitmaps.
0:06
With that out of the way let's
move on to the activities.
0:09
We've got main activity,
which was the list of pizzas.
0:12
And creator activity,
which let's us customize the pizzas.
0:16
Let's start with main activity.
0:20
At the top of the file,
we've got a pizza ID constant.
0:23
Other than that,
we just find the recyclerview.
0:27
Give it a layout manager, and
set it up with an adapter.
0:32
If you've not seen something
like this before, don't worry.
0:37
It's just an anonymous
function stored in a variable.
0:41
So launch creator activity is
a function that takes in an integer
0:45
which might be null and when this
function runs, it will create an intent,
0:51
add the pizza ID to the intent and
then start creator activity.
0:57
So, let's jump into Main Adapter and
start from the top.
1:05
You can use Cmd or
CTRL+B to jump to a class from its usage.
1:09
First, we've got our LaunchCreatorActivity
function as the only parameter.
1:14
And as we just saw it takes an integer
that might be null and returns nothing.
1:19
From here, we have a list of pizzas
which will contain the pizzas
1:25
to display on the screen.
1:29
Each row in this RecyclerView
will use the pizza row layout.
1:31
If we take a look at that layout,
under Resources and the Layout directory.
1:39
We can see that it contains a text
view for the name of the pizza,
1:45
a create new pizza button and
a divider line at the bottom.
1:49
So instead of using multiple views for
the pizza rows and
1:55
the create new pizza button, we just used
this one view and we'll hide the elements
1:58
we don't need depending on whether we
wanna show the pizza or the button.
2:04
Getting back to the adapter,
let's move onto the view holder.
2:09
Back in the code we can see that the item
count is one more than the size of
2:13
the list, this is to account for
the new pizza button at the bottom.
2:18
Moving on to the view holder, we show or
2:23
hide the views depending
on which type of row it is.
2:26
So, if it's not the last item in the list,
we'll hide the button and if it is
2:30
the last item in the list, we'll make the
button visible and hide everything else.
2:35
We also set up the on click
listeners to call through to our
2:41
launch creator activity function.
2:43
If it's a pizza row that was clicked,
we'll pass in the ID of the pizza.
2:46
And if the new pizza button was clicked,
we'll just pass in null.
2:53
So regardless of which row we click,
we'll end up launching CreatorActivity.
2:57
Let's hop over to CreatorActivity and
see where we go from here.
3:03
Let me give us just a little
more room here as well.
3:10
Let me just hide it instead.
3:15
At the top of CreatorActivity we
create a pizza ID variable and
3:19
lateinit it another
variable named pizzaView.
3:24
In Cotland, when you create a variable
you have to give it a value.
3:28
But with lateinit, we can get around that.
3:32
We just need to make sure it gets
initialized before we use it.
3:35
Speaking of PizzaView, let's take
a quick detour of the PizzaView class.
3:39
Remember, Cmd or Ctrl+B on the class
name will jump us into the class.
3:45
PizzaView is a custom view, meaning it
extends from View that draws a pizza.
3:51
It starts by drawing a big circle for
the crust.
3:57
Then it draws a smaller circle for
the body of the pizza.
4:01
And finally draws each
of the toppings on top.
4:05
To tell it which toppings to draw,
we pass in a map of toppings to Booleans.
4:10
If a topping is paired with
the value of true, we draw it.
4:16
Otherwise we don't and that's about all we
need to know about the PizzaView class.
4:19
Though, feel free to pause me and
4:25
look through the rest of
this code if you'd like.
4:26
Getting back to CreatorActivity and
the onCreate method,
4:30
we retrieve the Pizza ID
from the intent and
4:36
initialize our PizzaView to an empty
pizza, by passing in an empty map.
4:39
Later on when we have some data,
we'll update this map to not be empty.
4:45
From here we add our PizzaView to the
frameLayout at the top of the screen and
4:50
set up our recyclerView
that lives at the bottom.
4:55
To finish out CreatorActivity,
4:58
we've got a couple methods to set up
the menu at the top of the screen.
5:00
On create options menu,
creates the menu and
5:05
on options item selected is where we
add functionality to the menu items.
5:08
The last class we need to
look at is CreatorAdaptor.
5:13
This class manages the recyclerView
at the bottom of CreatorActivity.
5:21
And as a parameter,
it takes in a pizza view.
5:26
With this pizza view at the bottom,
when a switch is checked,
5:29
we update the toppings mapping
with the state of the switch and
5:35
tell the pizza view to update by
calling the invalidate method.
5:38
Awesome we've now got a good
understanding of the app, and
5:43
are ready to implement that resistance,
I know that was a lot to cover.
5:46
But don't worry, from here on out we'll
be taking things one step at at time.
5:50
And if you want to spend some
more time playing around and
5:55
getting comfortable with the app,
please do.
5:57
I'll be here waiting for
you when you get back.
5:59
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