YouSub
Untitled

en English

Untitled English subtitles

0 downloads · 73 lines · added Dec 6, 2015

Report a problem
What's wrong?
Advertisement

English transcript

Hooray!
You just made your app interactive for the user.
Before, when you tapped on the button, it didn't do anything.
But now, with some code changes,
when you tap on the button, it actually updates something on the screen.
Great job!
Let's try to dig deeper into what you just accomplished.
First you added a new android:onClick= attribute on the button XML element.
This means when the button is clicked on,
then it will try to perform or execute as we say in Java
a set of instructions that have been named submit order.
Okay. Going into the main activity
file that we gave you.
I know there's a lot of weird symbols and words on this screen.
But all you need to know now is that this is a method and
its name is submit order.
A method can contain multiple lines of java code.
And this translates into a set of instructions
that the device can understand and perform.
If there are multiple lines of code in this method,
the device would perform the first line first and then the second line and so
on until the bottom of the method.
The reason why we give methods a name is so that we can easily refer to it later.
For example, in the XML we can refer to the method submitOrder now.
It's sort of how I have a name, you have a name.
If you had a pet, you would also give it a name so
that you can easily refer to it.
So in code, if we have a bunch of statements that we want to refer to,
we can put it inside a method, and then give the method a name.
So remember in lesson one how we told you that XML has very specific syntax
rules that determine whether something is valid XML or not.
Well, Java has similar types of rules for determining what is valid Java code.
For methods, the statements inside of them must be contained within this
opening squiggly brace and this closing squiggly brace.
Up here, this is also a method called onCreate.
The statements inside this method are contained within this opening brace and
this closing brace.
And, lastly, there's this method, which is called display.
The code statements inside of it are marked off by this opening brace and
this closing brace.
You also notice that there's also an opening and closing brace for
the MainActivity.
All the functionality of the MainActivity
is defined within this opening and closing brace.
So far, this MainActivity contains three methods inside it.
So after you declare a method,
which means defining the method, then you can actually use that method.
In this example, we declare the display method, and
then we use it on this line of code, where we call it by its name.
So this is where we declare the method and this is where we call the method.
So to summarize, when the button is pressed in the UI,
then it triggers the submitOrder method to be called.
Then we execute each line of code in the submitOrder method
from the top of the method to the bottom of the method.
In this case, there's only one code statement in there, so we just run that.
Okay, now let's look at this line of code here that says display one.
The reason why I picked this line of code is because it seems like
the number inside the parenthesis is what gets displayed on the screen here.
So, when I click on this order button here, in the UI,
then it triggers the submit order method to be called in the main activity.
Then we perform this line of code here that says display 1.
And that updates the screen to show 1 here.
So if I change this to be 2 here, what do you think will happen?
Let's run the app to find out.
And now when I click on this button, it shows 2.
Clicking on the order button triggers the submit order method to be called.
And then we perform this line of code that says display 2.
And that's what updates the screen to show 2.
Now it's your turn.
Can you try changing this number here in parenthesis to a different number?
You can pick another small number, a big number, or even a negative number.
Once you've experimented with this line of code in the main activity
then check this box when you're done.

Need subtitles from another video?