SlideShare a Scribd company logo
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 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.
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;
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...
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>➪ ➪
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.
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
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
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());
}
});
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));
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));
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?

More Related Content

Viewers also liked

Mech eng presentation
Mech eng presentationMech eng presentation
Mech eng presentation
Vlad Kolesnyk
 
59828 employee benefits compliance checklist for small employers 021312
59828 employee benefits compliance checklist for small employers 02131259828 employee benefits compliance checklist for small employers 021312
59828 employee benefits compliance checklist for small employers 021312
Jerry Whitaker CIC,CRIS
 
Sayed Taha CV
Sayed Taha CVSayed Taha CV
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
SNichols4
 
Dairyfree
DairyfreeDairyfree
Dairyfree
Missdip24
 
Git
GitGit
Android tutorials8 todo_list
Android tutorials8 todo_listAndroid tutorials8 todo_list
Android tutorials8 todo_list
Vlad Kolesnyk
 
Our encounter with d8
Our encounter with d8Our encounter with d8
Our encounter with d8
AddWeb Solution Pvt. Ltd.
 
Nota Tajuk 2 evolusi perkembangan PTV
Nota Tajuk 2 evolusi perkembangan PTVNota Tajuk 2 evolusi perkembangan PTV
Nota Tajuk 2 evolusi perkembangan PTV
Sherly Jewinly
 
Help your scrum team strike oil!
Help your scrum team strike oil!Help your scrum team strike oil!
Help your scrum team strike oil!
Michael O'Reilly
 
HAINUTE BOTEZ
HAINUTE BOTEZHAINUTE BOTEZ
Teks Forum Hegemoni
Teks Forum HegemoniTeks Forum Hegemoni
Teks Forum Hegemoni
Sherly Jewinly
 
4 dr speroff
4 dr speroff4 dr speroff
4 dr speroffmediasav7
 
Year 2015
Year 2015Year 2015
Mousedeer
Mousedeer Mousedeer
Mousedeer
mousedeer2e1
 
Android tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirerAndroid tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirer
Vlad Kolesnyk
 

Viewers also liked (17)

Mech eng presentation
Mech eng presentationMech eng presentation
Mech eng presentation
 
59828 employee benefits compliance checklist for small employers 021312
59828 employee benefits compliance checklist for small employers 02131259828 employee benefits compliance checklist for small employers 021312
59828 employee benefits compliance checklist for small employers 021312
 
Sayed Taha CV
Sayed Taha CVSayed Taha CV
Sayed Taha CV
 
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
Doing things with manuscripts: CLIR post doc-seminar, Bryn Mawr College 7.31....
 
Yourprezi
YourpreziYourprezi
Yourprezi
 
Dairyfree
DairyfreeDairyfree
Dairyfree
 
Git
GitGit
Git
 
Android tutorials8 todo_list
Android tutorials8 todo_listAndroid tutorials8 todo_list
Android tutorials8 todo_list
 
Our encounter with d8
Our encounter with d8Our encounter with d8
Our encounter with d8
 
Nota Tajuk 2 evolusi perkembangan PTV
Nota Tajuk 2 evolusi perkembangan PTVNota Tajuk 2 evolusi perkembangan PTV
Nota Tajuk 2 evolusi perkembangan PTV
 
Help your scrum team strike oil!
Help your scrum team strike oil!Help your scrum team strike oil!
Help your scrum team strike oil!
 
HAINUTE BOTEZ
HAINUTE BOTEZHAINUTE BOTEZ
HAINUTE BOTEZ
 
Teks Forum Hegemoni
Teks Forum HegemoniTeks Forum Hegemoni
Teks Forum Hegemoni
 
4 dr speroff
4 dr speroff4 dr speroff
4 dr speroff
 
Year 2015
Year 2015Year 2015
Year 2015
 
Mousedeer
Mousedeer Mousedeer
Mousedeer
 
Android tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirerAndroid tutorials7 calculator_packageexploirer
Android tutorials7 calculator_packageexploirer
 

Similar to Android tutorials7 calculator_javaprogramming

Create yourfirstandroidapppdf
Create yourfirstandroidapppdfCreate yourfirstandroidapppdf
Create yourfirstandroidapppdf
murad3003
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
MikialeTesfamariam
 
Calculator 2
Calculator 2Calculator 2
Calculator 2
livecode
 
Day 5
Day 5Day 5
Information about Toggle Button.pdf
Information about Toggle Button.pdfInformation about Toggle Button.pdf
Information about Toggle Button.pdf
Nishaadequateinfosof
 
XMetaL Dialog Odds & Ends
XMetaL Dialog Odds & EndsXMetaL Dialog Odds & Ends
XMetaL Dialog Odds & Ends
XMetaL
 
Gui programming a review - mixed content
Gui programming   a review - mixed contentGui programming   a review - mixed content
Gui programming a review - mixed content
Yogesh Kumar
 
Intro to ios - init by SLOHacks
Intro to ios - init by SLOHacksIntro to ios - init by SLOHacks
Intro to ios - init by SLOHacks
Randy Scovil
 
Java calculator
Java calculatorJava calculator
Java calculator
Sarah McNellis
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
Lilia Sfaxi
 
Chapter9 r studio2
Chapter9 r studio2Chapter9 r studio2
Chapter9 r studio2
Syracuse University
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
Aimee Maree Forsstrom
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
Ahsanul Karim
 
Violet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole TeamViolet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole Team
Anton Caceres
 
Notes netbeans
Notes netbeansNotes netbeans
Notes netbeans
poonamchopra7975
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
veeracynixit
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
veeracynixit
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Dadangsachir WANDA ir.mba
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
Basic calculator tutorial
Basic calculator tutorialBasic calculator tutorial
Basic calculator tutorial
Gilkye
 

Similar to Android tutorials7 calculator_javaprogramming (20)

Create yourfirstandroidapppdf
Create yourfirstandroidapppdfCreate yourfirstandroidapppdf
Create yourfirstandroidapppdf
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
 
Calculator 2
Calculator 2Calculator 2
Calculator 2
 
Day 5
Day 5Day 5
Day 5
 
Information about Toggle Button.pdf
Information about Toggle Button.pdfInformation about Toggle Button.pdf
Information about Toggle Button.pdf
 
XMetaL Dialog Odds & Ends
XMetaL Dialog Odds & EndsXMetaL Dialog Odds & Ends
XMetaL Dialog Odds & Ends
 
Gui programming a review - mixed content
Gui programming   a review - mixed contentGui programming   a review - mixed content
Gui programming a review - mixed content
 
Intro to ios - init by SLOHacks
Intro to ios - init by SLOHacksIntro to ios - init by SLOHacks
Intro to ios - init by SLOHacks
 
Java calculator
Java calculatorJava calculator
Java calculator
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Chapter9 r studio2
Chapter9 r studio2Chapter9 r studio2
Chapter9 r studio2
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Violet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole TeamViolet Peña - Storybook: A React Tool For Your Whole Team
Violet Peña - Storybook: A React Tool For Your Whole Team
 
Notes netbeans
Notes netbeansNotes netbeans
Notes netbeans
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
 
Ios actions and outlets
Ios actions and outletsIos actions and outlets
Ios actions and outlets
 
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
Enrich enriching mathematics conversi biner 16 pada sisikomputerize indoensia...
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
Basic calculator tutorial
Basic calculator tutorialBasic calculator tutorial
Basic calculator tutorial
 

More from Vlad Kolesnyk

Mobile andwebapps
Mobile andwebappsMobile andwebapps
Mobile andwebapps
Vlad Kolesnyk
 
Android tutorials7 calculator_intro
Android tutorials7 calculator_introAndroid tutorials7 calculator_intro
Android tutorials7 calculator_intro
Vlad Kolesnyk
 
Android tutorials8 todo_list
Android tutorials8 todo_listAndroid tutorials8 todo_list
Android tutorials8 todo_list
Vlad Kolesnyk
 
Android tutorials6 run_your_app
Android tutorials6 run_your_appAndroid tutorials6 run_your_app
Android tutorials6 run_your_app
Vlad Kolesnyk
 
Android tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipseAndroid tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipse
Vlad Kolesnyk
 
Android tutorials1 install_ide
Android tutorials1 install_ideAndroid tutorials1 install_ide
Android tutorials1 install_ide
Vlad Kolesnyk
 
Android tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayoutAndroid tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayout
Vlad Kolesnyk
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
Vlad Kolesnyk
 
Github tutorial1
Github tutorial1Github tutorial1
Github tutorial1
Vlad Kolesnyk
 

More from Vlad Kolesnyk (9)

Mobile andwebapps
Mobile andwebappsMobile andwebapps
Mobile andwebapps
 
Android tutorials7 calculator_intro
Android tutorials7 calculator_introAndroid tutorials7 calculator_intro
Android tutorials7 calculator_intro
 
Android tutorials8 todo_list
Android tutorials8 todo_listAndroid tutorials8 todo_list
Android tutorials8 todo_list
 
Android tutorials6 run_your_app
Android tutorials6 run_your_appAndroid tutorials6 run_your_app
Android tutorials6 run_your_app
 
Android tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipseAndroid tutorials2 android_tools_on_eclipse
Android tutorials2 android_tools_on_eclipse
 
Android tutorials1 install_ide
Android tutorials1 install_ideAndroid tutorials1 install_ide
Android tutorials1 install_ide
 
Android tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayoutAndroid tutorials7 calculator_basiclayout
Android tutorials7 calculator_basiclayout
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
 
Github tutorial1
Github tutorial1Github tutorial1
Github tutorial1
 

Recently uploaded

How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 

Recently uploaded (20)

How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 

Android tutorials7 calculator_javaprogramming

  • 2. All java programming relatively to functionality of your app is done in MainActivity.java type of files.
  • 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?