Question: ICT 3 7 2 Mobile Computing Tutorial 0 5 Topic: Week 5 : OnClickListener: Android Calculator Application Submission: Five minutes before the end of the
ICT Mobile Computing
Tutorial
Topic: Week : OnClickListener: Android Calculator Application
Submission: Five minutes before the end of the tutorial: all files must be uploaded on Moodle.
Exercise : Add functionality to Calculator Application
Create a layout like this in Android Studio Use the layout that you have created in
Tutorial
Add all the functionalities to the calculator.
Create a new project in Android Studio or open your existing project if you have
already created the layout and go to step Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
Select Empty Activity template.
Name the applications as MyCalculator. Select java as language and press Finish.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
After clicking Finish, it takes around ~ minutes to build Activity and files.
Lets design the UI of activitymain.xml Go to Res Layout activitymain
Open activitymain.xml navigate to code section and remove the existing code.
Add this code
Now go back to design section.
Drag and drop all the buttons in the layout.
For button set ID as button
For button set ID as button
For button set ID as button For button set ID as button For
button set ID as button For button set ID as button For button Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
set ID as button For button set ID as
button For button set ID as button
For button set ID as button
For button set ID as buttonadd
For button set ID as buttonsub
For button x set ID as buttonmul
For button set ID as buttondiv
For button C set ID as buttonC
For button set ID as button
For button set ID as buttoneql
Drag and drop a TextView at top and set its ID as textViewresult.
Now we will add our Java code in our MainActivity.java file.
So open you MainActivity.java file from left side of IDE
app java com.example.mycalculator MainActivity.java
Now declare a button and EditText type of variables in MainActivity. Add the
following code.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
Now we will attach our layout buttons with these button variables by using their
IDs. buttonButton findViewByIdRidbutton; Add the following code for
all Buttons and a TextView.
Now we will add number for each button click. If user is clicking on button it
should display in TextView.
Add onClickListener for all the buttons Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
Now lets start working on addition function. First declare two float values and four
Boolean values.
Now add this onClicklistener for buttonAdd at the end.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
In the same way add onClicklistener for buttonSub, buttonMul, buttonDivision.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
Now we will add code for ButtonEqual. Add the following code at the end.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
ICT Tutorial Compiled by: Madiha Anjum
Now last part is to add functionality to C button. Add the following code at the end.
Now run the applications and test all the buttons.
This is how output should look like.Information extracted from: Smyth N Android Studio Development Essentials Java Edition
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
