Advertisement
English transcript
I want you to build up this layout on your own.
You'll add one text view for the price header here and
then another text view to show zero dollars.
Also assign this second text view a view ID of price_text_view.
The reason why I ask you to add an ID is because we're going to need to
reference this view later so we can update the price.
And while you're at it, also make the style of these two text views similar to
the quantity text views.
In terms of vertical spacing, we also have 16dp in between these views.
In order for the price field to also update,
we're going to need to make some changes to the main activity Java file.
We'll give you a little more help with that.
We're going to provide you with a new method that you can add in the main
activity Java file.
Open up the gist in the link provided in the instructor notes.
By the way, a gist means a snippet or
a short region of reusable code that you want to share with others.
In this case, we went to the GitHub Gist webpage, and
we created a new Gist with this code that we wanted to give you.
And now we can just share this link with you, and you can get the code.
For this snippet, we're going to select the whole thing, and then copy it.
Remember to include the comment up here,
which is a note to the reader and this closing brace down here.
Now we're going to paste it in the main activity Java file.
I love free code.
Make sure that the braces still line up after you paste it.
Braces are a pain whether they're in your app or on your teeth.
You can get a ton of errors if your braces don’t line up..
For example, if I paste this code block right down here,
then I get an error saying class or interface expected.
The reason is because this method is hanging out in the middle of nowhere.
Really, we want it to be inside the main activity between the opening and
the closing braces of the main activity.
So, let's copy this and put it before the closing brace of the main activity.
And that's better.
Another potential error is if you forget to copy the closing brace, and
you just grab this top part of the method.
In that case,
you would get another error that says that the closing brace is expected.
So you would need to add that back in.
The other potential error you might run into is if you accidentally
add the method right here.
This is before the closing brace of the display method.
So now this whole thing is inside the display method,
which is not what we want.
We want it to be beside the display method, right out here.
You get the idea, there's a million combinations.
You just want to be careful and make sure that your braces line up.
Another good way to check is if you put your cursor right here,
right after the opening brace you can see that it highlights the closing brace
that matches with it.
Same here, if I click on this,
the closing brace that matches with this is right here.
Same for submitOrder method and onCreate method and even the MainActivity.
This is the opening brace for the MainActivity, and
the closing brace is highlighted in blue all the way down here.
Now unfortunately, we still have another error here.
If I look in the side bar here it says cannot resolve symbol number format.
Number format is actually something provided by Android
in order to help format numbers.
In this case we're trying to format the number which is the price so
that it has a dollar sign, or whatever local currency you're using.
We need to add an import statement for
this, because currently Android Studio can't recognize what this is.
To resolve this I'm going to show you how to turn on auto import.
In the menu across the top of Android Studio,
click on Android Studio > Preferences.
This opens up the preferences for Android Studio.
There's a ton of preferences that I've never changed, but
if you ever need something I just search it using this search box.
So I'm going to type in Auto Import.
Now, it shows the settings for
automatically importing things in XML and Java.
I want to check this box here that says, Add Unambiguous Imports on the fly.
This phrase may not mean anything to you, but that's okay.
This phrase probably sounds really confusing.
All it's doing is telling Android Studio that it's okay to automatically add
some additional lines of code in our app.
Specifically import statements, if it knows what we're referring to.
Let me show you.
Well actually, it already did it for us.
It assumed that when we said number format, we meant this one and
it is correct.
So it solved the problem for us automatically.
Which is great.
Okay. At this point, if you run the app,
then when you click on the Order button then the quantity changes, but
not the price.
We need to modify the submit order method so
that it updates the quantity as well as the price.
So let's add another line of code.
I'm going to call the display price method and I'm going to
say that the price is two cups of coffee times $5 each.
The reason why our coffee is $5 each is because we've
imported Arabica beans from Ethiopia.
But anyways, we're going to let the device do the calculation for
us, because we have two cups of coffee at $5 each.
By the way, when I refer to dollars in this lesson,
feel free to swap it out for your own local currency.
There's no dollar sign in this math expression.
It gets added on later in the display price method.
But don't worry about the details of the display price method,
we're going to be going over that in lesson three.
For now just focus on the math expression that you're going to
use to calculate the price.
Also remember the semicolon at the end of each statement, otherwise
the semicolon monster is going to come and get you and give you a red error.
For example, when you remove the semi colon, then you get an error that says,
that the semi colon is expected.
Oh, and to be extra, extra clear, the semi colon key, is this one right here.
It's not the colon, it's the semi colon.
All right, so when you finish everything,
you should have two more text views added for the price here.
You should have modified the main activity to include
the new method we gave you.
And you would've also modified the submitOrder method
to add a new line of code to display the price.
When you run it on your device it should look something like this.
It starts off at zero quantity for $0.
And when you tap on it, there's now two coffees ordered for $10.
Feel free to experiment with different values for quantity and price.
If you want to charge $10 for a cup of coffee, you can go right ahead.
Make sure you switch it back to two cups of coffee at $5 each so
that it matches our code going forward.
Check these boxes when you are done.
You'll add one text view for the price header here and
then another text view to show zero dollars.
Also assign this second text view a view ID of price_text_view.
The reason why I ask you to add an ID is because we're going to need to
reference this view later so we can update the price.
And while you're at it, also make the style of these two text views similar to
the quantity text views.
In terms of vertical spacing, we also have 16dp in between these views.
In order for the price field to also update,
we're going to need to make some changes to the main activity Java file.
We'll give you a little more help with that.
We're going to provide you with a new method that you can add in the main
activity Java file.
Open up the gist in the link provided in the instructor notes.
By the way, a gist means a snippet or
a short region of reusable code that you want to share with others.
In this case, we went to the GitHub Gist webpage, and
we created a new Gist with this code that we wanted to give you.
And now we can just share this link with you, and you can get the code.
For this snippet, we're going to select the whole thing, and then copy it.
Remember to include the comment up here,
which is a note to the reader and this closing brace down here.
Now we're going to paste it in the main activity Java file.
I love free code.
Make sure that the braces still line up after you paste it.
Braces are a pain whether they're in your app or on your teeth.
You can get a ton of errors if your braces don’t line up..
For example, if I paste this code block right down here,
then I get an error saying class or interface expected.
The reason is because this method is hanging out in the middle of nowhere.
Really, we want it to be inside the main activity between the opening and
the closing braces of the main activity.
So, let's copy this and put it before the closing brace of the main activity.
And that's better.
Another potential error is if you forget to copy the closing brace, and
you just grab this top part of the method.
In that case,
you would get another error that says that the closing brace is expected.
So you would need to add that back in.
The other potential error you might run into is if you accidentally
add the method right here.
This is before the closing brace of the display method.
So now this whole thing is inside the display method,
which is not what we want.
We want it to be beside the display method, right out here.
You get the idea, there's a million combinations.
You just want to be careful and make sure that your braces line up.
Another good way to check is if you put your cursor right here,
right after the opening brace you can see that it highlights the closing brace
that matches with it.
Same here, if I click on this,
the closing brace that matches with this is right here.
Same for submitOrder method and onCreate method and even the MainActivity.
This is the opening brace for the MainActivity, and
the closing brace is highlighted in blue all the way down here.
Now unfortunately, we still have another error here.
If I look in the side bar here it says cannot resolve symbol number format.
Number format is actually something provided by Android
in order to help format numbers.
In this case we're trying to format the number which is the price so
that it has a dollar sign, or whatever local currency you're using.
We need to add an import statement for
this, because currently Android Studio can't recognize what this is.
To resolve this I'm going to show you how to turn on auto import.
In the menu across the top of Android Studio,
click on Android Studio > Preferences.
This opens up the preferences for Android Studio.
There's a ton of preferences that I've never changed, but
if you ever need something I just search it using this search box.
So I'm going to type in Auto Import.
Now, it shows the settings for
automatically importing things in XML and Java.
I want to check this box here that says, Add Unambiguous Imports on the fly.
This phrase may not mean anything to you, but that's okay.
This phrase probably sounds really confusing.
All it's doing is telling Android Studio that it's okay to automatically add
some additional lines of code in our app.
Specifically import statements, if it knows what we're referring to.
Let me show you.
Well actually, it already did it for us.
It assumed that when we said number format, we meant this one and
it is correct.
So it solved the problem for us automatically.
Which is great.
Okay. At this point, if you run the app,
then when you click on the Order button then the quantity changes, but
not the price.
We need to modify the submit order method so
that it updates the quantity as well as the price.
So let's add another line of code.
I'm going to call the display price method and I'm going to
say that the price is two cups of coffee times $5 each.
The reason why our coffee is $5 each is because we've
imported Arabica beans from Ethiopia.
But anyways, we're going to let the device do the calculation for
us, because we have two cups of coffee at $5 each.
By the way, when I refer to dollars in this lesson,
feel free to swap it out for your own local currency.
There's no dollar sign in this math expression.
It gets added on later in the display price method.
But don't worry about the details of the display price method,
we're going to be going over that in lesson three.
For now just focus on the math expression that you're going to
use to calculate the price.
Also remember the semicolon at the end of each statement, otherwise
the semicolon monster is going to come and get you and give you a red error.
For example, when you remove the semi colon, then you get an error that says,
that the semi colon is expected.
Oh, and to be extra, extra clear, the semi colon key, is this one right here.
It's not the colon, it's the semi colon.
All right, so when you finish everything,
you should have two more text views added for the price here.
You should have modified the main activity to include
the new method we gave you.
And you would've also modified the submitOrder method
to add a new line of code to display the price.
When you run it on your device it should look something like this.
It starts off at zero quantity for $0.
And when you tap on it, there's now two coffees ordered for $10.
Feel free to experiment with different values for quantity and price.
If you want to charge $10 for a cup of coffee, you can go right ahead.
Make sure you switch it back to two cups of coffee at $5 each so
that it matches our code going forward.
Check these boxes when you are done.