Skip to content

Exercise 04 - Sum Calculator

Project name : E04 Sum Calculator

Introduction

The purpose of this exercise is to learn how to use Layout Editor and handle UI events. You will need to do an application, where end user can add numbers together - like a sum calculator. Remember check that your application UI is working with portrait and landscape orientations.

Example video

Look below example video to get a point what you need to do.

How to work

  • Create a new project and name it to E04 Sum Calculator, select Kotlin language
  • Remember save your project in your android-exercises folder
  • Think what layout manager would work best for the buttons and use that one
  • It is enough to get addition (sum) to work (or just a layout in landscape and portrait)

Small tips

  • Add all the number buttons to call the same function android:onClick="numberInput"
  • You can get number from the pressed button with below code

1
2
3
4
5
fun numberInput(view: View) {
  // view is a button (pressed one) get text and convert to Int
  val digit = (view as Button).text.toString().toInt()
  ...
}
* You can add a new number to textView with below code

1
2
// append a new string to textView
textView.append(digit.toString())
  • You might need a few class based variables to hold given number and sum
1
2
3
4
5
// clicked number, like 55
private var number: Int = 0

// calculated sum
private var sum: Int = 0
  • And an event handling and logic for the +, = and c buttons

Problem with calculation?

If you don't get sum to work, you can return this exercise if both portrait and landscape layouts works correctly.

Test and push

Test your application in emulator, take screenshots and commit/push your project and screenshots back to JAMKIT/GitLab. Remember move your exercise/issue ticket from Doing to In Review in Issues Board and write your learning comments to issue comments.