3. I am not going to explain the
way functions work here in
detail yet. (they are
generated automatically)
All we need to know is that
all code inside onCreate is
running when program first
starts.As a first step we need to
create variables which will
represent the value of input
numbers and the final
result.
4. Hence, create three integer
numbers.
In java each variable has a
type.
Integer number has a type of
int.
E.g. integer number with
name firstVar:
int firstVar;
5. Hence, create 4 buttons
variables, 2 EditText and 1
TextView for each layout
item.
As well as to represent
numbers we should set a
type for Button, TextView
and EditText.
E.g. Button with name 'plus':
Button plus;
TextView with name
'answer':
TextView answer;
and so on...
6. As you noticed Button,
TextView and EditText are
underlined this mean that
they have an error.
Eclipse/Android Studio
doesn't understand what
does they mean until we
import library.You can do it by navigating
your mouse on it and
pressing import.
Or you can import them all on Eclipse
by running <ctrl> <shift> <all>➪ ➪
7. It is easy to give a value to
int variable, just e.g.
firstVar=0;
Now we need to initialized our
variables, give them value.
We have to do it inside onCreate
function, so we will have them
initialized when app is just created.
In order to initialize Button, EditText
e.g. for button variable 'add':
add = (Button)
findViewById(R.id.plus);
where 'plus' is id we set it up in layout
file.
8. Create onClickListener for
all buttons.
Now let's set button click
listener for all buttons.
You can do it by adding
.setOnClickListner
attribute which creates
implemented method
onClick, which is running
when the button (e.g.
plus) is clicked
9. plus.setOnClickListener(new
View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
minus.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
mult.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
}
});
div.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
stub
}
});
Next step is to add
functionality when buttons
will be pressed
10. Firstly lets get the value of our variables from the EditText form.
We are getting the text inside EditText in type String(type for text)
by writing:
firstInput.getText().toString()
In order to translate string value into type int(type for math
arithmetics) we write:
Integer.parseInt(<code which gives us string variable>)
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
}
});
11. This line makes in this case addition of 2 variables and put it into
the result variable.
result = firstVar + secondVar;
This line sets the result value translated into String type within
EditText form <answer>
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
result = firstVar + secondVar;
}
});
answer.setText(String.valueOf(result));
answer.setText(String.valueOf(result));
12. Now change each onClickListener action relatively to the button
arithmetical operation and you can run the app.
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
result = firstVar + secondVar;
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
result = firstVar - secondVar;
}
});
mult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
result = firstVar * secondVar;
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
firstVar = Integer.parseInt(firstInput.getText().toString());
secondVar = Integer.parseInt(secondInput.getText().toString());
result = firstVar / secondVar;
}
});
answer.setText(String.valueOf(result));
answer.setText(String.valueOf(result));
answer.setText(String.valueOf(result));
answer.setText(String.valueOf(result));
13. Put numbers and click on
+,-,/ or *.
Does it work only if you
put numbers first?
Can you fix it?
Can you make
the code
shorter?