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

ICT372 Mobile Computing
Tutorial 05
Topic: Week 5: OnClickListener: Android Calculator Application
Submission: Five minutes before the end of the tutorial: all files must be uploaded on Moodle.
Exercise 1: Add functionality to Calculator Application
1. Create a layout like this in Android Studio (Use the layout that you have created in
Tutorial 1)
2. Add all the functionalities to the calculator.
3. Create a new project in Android Studio or open your existing project if you have
already created the layout and go to step 12.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
4. Select Empty Activity template.
5. Name the applications as MyCalculator. Select java as language and press Finish.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
6. After clicking Finish, it takes around ~2 minutes to build Activity and files.
7. Lets design the UI of activity_main.xml. Go to Res > Layout > activity_main
8. Open activity_main.xml, navigate to code section and remove the existing code.
Add this code
9. Now go back to design section.
10. Drag and drop all the buttons in the layout.
For button 1 set ID as button1
For button 2 set ID as button2
For button 3 set ID as button3 For button 4 set ID as button4 For
button 5 set ID as button5 For button 6 set ID as button6 For button 7Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
set ID as button7 For button 8 set ID as
button8 For button 9 set ID as button9
For button 0 set ID as button0
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 button10
For button = set ID as buttoneql
11. Drag and drop a TextView at top and set its ID as textView_result.
12. Now we will add our Java code in our MainActivity.java file.
13. So open you MainActivity.java file from left side of IDE
(app -> java -> com.example.mycalculator-> MainActivity.java)
14. Now declare a button and EditText type of variables in MainActivity. Add the
following code.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
15. Now we will attach our layout buttons with these button variables by using their
IDs. ( button0=(Button) findViewById(R.id.button0);) Add the following code for
all Buttons and a TextView.
16. Now we will add number for each button click. If user is clicking on button 1 it
should display 1 in TextView.
17. Add onClickListener for all the buttons (0,1,2,3,4,5,6,7,8,9,.)Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
18. Now lets start working on addition function. First declare two float values and four
Boolean values.
19. Now add this onClicklistener for buttonAdd at the end.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
20. In the same way add onClicklistener for buttonSub, buttonMul, buttonDivision.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
21. Now we will add code for ButtonEqual. Add the following code at the end.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition
ICT372 Tutorial 04 Compiled by: Madiha Anjum
22. Now last part is to add functionality to C button. Add the following code at the end.
23. Now run the applications and test all the buttons.
24. This is how output should look like.Information extracted from: Smyth, N. Android Studio 3.5 Development Essentials Java Edition

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!