Your SlideShare is downloading. ×
0
Basic Java programming
in Android
All java programming
relatively to functionality of
your app is done in
MainActivity.java type of
files.
I am not going to explain the
way functions work here in
detail yet. (they are
generated automatically)
All we need to kno...
Hence, create three integer
numbers.
In java each variable has a
type.
Integer number has a type of
int.
E.g. integer numb...
Hence, create 4 buttons
variables, 2 EditText and 1
TextView for each layout
item.
As well as to represent
numbers we shou...
As you noticed Button,
TextView and EditText are
underlined this mean that
they have an error.
Eclipse/Android Studio
does...
It is easy to give a value to
int variable, just e.g.
firstVar=0;
Now we need to initialized our
variables, give them valu...
Create onClickListener for
all buttons.
Now let's set button click
listener for all buttons.
You can do it by adding
.setO...
plus.setOnClickListener(new
View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated meth...
Firstly lets get the value of our variables from the EditText form.
We are getting the text inside EditText in type String...
This line makes in this case addition of 2 variables and put it into
the result variable.
result = firstVar + secondVar;
T...
Now change each onClickListener action relatively to the button
arithmetical operation and you can run the app.
plus.setOn...
Put numbers and click on
+,-,/ or *.
Does it work only if you
put numbers first?
Can you fix it?
Can you make
the code
sho...
Upcoming SlideShare
Loading in...5
×

Android tutorials7 calculator_javaprogramming

155

Published on

Published in: Technology, Art & Photos
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
155
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Android tutorials7 calculator_javaprogramming"

  1. 1. Basic Java programming in Android
  2. 2. All java programming relatively to functionality of your app is done in MainActivity.java type of files.
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×