Advertisement
English transcript
I think you're ready for the next step of building the quantity picker.
In our order form we're going to add a plus and a minus button so
that we can increase and decrease the number of coffees ordered.
Each time you hit the plus button,
it will increase the number of coffees by one.
And each time you hit the minus button,
it will decrease the number of coffees by one.
Then when you hit the order button,
it will update the price according to the latest quantity value.
Building up all of this functionality properly will take multiple steps.
For the first task we're going to add the plus and
the minus buttons to the layout.
And we'll also hook up the buttons so
they actually update something on the screen.
And even later in the lesson we're going to fix the order button so
that the price updates properly.
First, build this layout by modifying the activity-main layout XML file.
You'll add the two buttons and also please change the initial value of
the quantity to be 2 and the initial price to be $10.
You can make these changes in the XML as well.
You can adjust to your local currency where applicable.
The reason why I'm choosing two as a quantity is so
that when you hit the minus button, it doesn't go negative.
When you add these buttons to your layout you'll notice that they appear to
be a little bit bigger than they are showing up here.
You can achieve this look by setting the width and the height to be 48 dps.
Then hook up the on click behavior.
When the + button is clicked, call the increment method in the main activity.
When the- button is clicked call the decrement method in the main activity.
Within the main activity add an increment method.
Inside that method create a quantity variable and
initialize it to the value 3.
When I say initialize it just means to set the initial value of the variable to
be 3.
Then go ahead and display the quantity to the screen.
For the third step in the main activity also add a decrement method and
create a quantity variable that's initialized to the value 1.
Then go ahead and display the quantity to the screen.
After you've completed all those steps this is what it should look like
on the device.
When you open the app it should have a quantity of 2 and a price of $10.
If you hit the plus button, it increases the quantity to 3.
If you hit the minus button, then it decreases the quantity to 1.
Technically, that behavior is incorrect because it should've gone down to 2.
But for now, just make the plus button always change it to 3 and
the minus button always change it to 1.
In a later coding task, we're going to fix it so
that every time you hit the plus button, it will keep increasing to 4, 5,
6, etc., and same for the minus button.
For now, you can leave the price and the order button as is.
Right now, my version just updates the quantity to 5 and the price to $25, but
you can leave it as whatever you had before.
We're also going to fix this later.
As a helpful tip, if you ever want to go back to how the app
was when it first launched, you can always kill your app.
In the system navigation bar, there's a couple of buttons, back, home, and
recent tasks.
If you hit this recent tasks button,
it shows all the recent tasks that you are doing on your phone.
If you swipe to the left or
right it kills your app, that means it stops it from running.
If you go into the all apps screen and then click on your app to
launch it again, then it shows the initial state of the app
In our order form we're going to add a plus and a minus button so
that we can increase and decrease the number of coffees ordered.
Each time you hit the plus button,
it will increase the number of coffees by one.
And each time you hit the minus button,
it will decrease the number of coffees by one.
Then when you hit the order button,
it will update the price according to the latest quantity value.
Building up all of this functionality properly will take multiple steps.
For the first task we're going to add the plus and
the minus buttons to the layout.
And we'll also hook up the buttons so
they actually update something on the screen.
And even later in the lesson we're going to fix the order button so
that the price updates properly.
First, build this layout by modifying the activity-main layout XML file.
You'll add the two buttons and also please change the initial value of
the quantity to be 2 and the initial price to be $10.
You can make these changes in the XML as well.
You can adjust to your local currency where applicable.
The reason why I'm choosing two as a quantity is so
that when you hit the minus button, it doesn't go negative.
When you add these buttons to your layout you'll notice that they appear to
be a little bit bigger than they are showing up here.
You can achieve this look by setting the width and the height to be 48 dps.
Then hook up the on click behavior.
When the + button is clicked, call the increment method in the main activity.
When the- button is clicked call the decrement method in the main activity.
Within the main activity add an increment method.
Inside that method create a quantity variable and
initialize it to the value 3.
When I say initialize it just means to set the initial value of the variable to
be 3.
Then go ahead and display the quantity to the screen.
For the third step in the main activity also add a decrement method and
create a quantity variable that's initialized to the value 1.
Then go ahead and display the quantity to the screen.
After you've completed all those steps this is what it should look like
on the device.
When you open the app it should have a quantity of 2 and a price of $10.
If you hit the plus button, it increases the quantity to 3.
If you hit the minus button, then it decreases the quantity to 1.
Technically, that behavior is incorrect because it should've gone down to 2.
But for now, just make the plus button always change it to 3 and
the minus button always change it to 1.
In a later coding task, we're going to fix it so
that every time you hit the plus button, it will keep increasing to 4, 5,
6, etc., and same for the minus button.
For now, you can leave the price and the order button as is.
Right now, my version just updates the quantity to 5 and the price to $25, but
you can leave it as whatever you had before.
We're also going to fix this later.
As a helpful tip, if you ever want to go back to how the app
was when it first launched, you can always kill your app.
In the system navigation bar, there's a couple of buttons, back, home, and
recent tasks.
If you hit this recent tasks button,
it shows all the recent tasks that you are doing on your phone.
If you swipe to the left or
right it kills your app, that means it stops it from running.
If you go into the all apps screen and then click on your app to
launch it again, then it shows the initial state of the app