Exercise: Calculator

Program and Android application that do simple calculation.

In this first version it does only '+' on integers. In a later exercise you will make another, more flexible calculator.

This user interfaces consists of

The layout does not have to be as nice as the above - you will make it look nicer very soon ...

When the user clicks the '=' button, the result is calculated and displayed: Make an onClick listener.

Hint: Integer.parseInt(someString) might be useful.

Next version: Decimal numbers calculator

Change the calculator to use decimal numbers, not integers.

  1. In the XML file change the android:inputType to allow decimal numbers (positive and negative)
  2. Change the Java code to use Double.parseDouble(someString)

Better layout

To make the layout look better, like the screen shot at the start of this exercise:

Give the two input fields and the result field all awailable space.

  1. Change the RelativeLayout to LinearLayout
  2. Add this to the "big" fields, android:layout_weight="1"
  3. android:textAlignment="center" might also be helpful for some of the fields

Experiment: Change screen orientation

You have probably tried the calculator application using portrait orientation. Try to change to landscape orientation on your phone or emulator.