• We just launched and are currently in beta. Join us as we build and grow the community.

Calculator Mobile App (Android, iOS, WinPhone)

sezoka

UX/UI Master
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
171
Likes
168
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This is a simple demo of a functional calculator native mobile application. It is written using Codename One, and can be deployed as a native Android, iOS, Windows Phone, J2ME, BlackBerry, Web, Mac, or Windows App.

The code is as follows:

  1. package

    com.codename1.demos.calculator

    ;


  2. import

    com.codename1.ui.Button

    ;
  3. import

    com.codename1.ui.Component

    ;
  4. import

    com.codename1.ui.Container

    ;
  5. import

    com.codename1.ui.Display

    ;
  6. import

    com.codename1.ui.Form

    ;
  7. import

    com.codename1.ui.TextField

    ;
  8. import

    com.codename1.ui.layouts.BorderLayout

    ;
  9. import

    com.codename1.ui.layouts.GridLayout

    ;
  10. import

    com.codename1.ui.plaf.UIManager

    ;
  11. import

    com.codename1.ui.util.Resources

    ;
  12. import

    java.io.IOException

    ;

  13. /**
  14. * This is a demo Calculator app that performs simple Math. Its purpose
  15. * is to demonstrate how to create a simple yet functional GUI in a mobile
  16. * application that runs on iOS, Android, Javascript,
  17. * @author shannah
  18. */
  19. public

    class

    Calculator {

  20. private

    Form current;

  21. private

    Resources theme;

  22. /**
  23. * The current operation
  24. * '+', '-', '*', or '/'
  25. */
  26. private

    char

    op;

  27. /**
  28. * The current calculated value
  29. */
  30. private

    double

    currVal;

  31. /**
  32. * The input field
  33. */
  34. TextField

    inputField;

  35. /**
  36. * Flag to indicate that the value has just been reset
  37. */
  38. boolean

    reset =

    true

    ;

  39. /**
  40. * Flag to indicate that input mode is to append digits.
  41. * If false, the next digit pressed will replace whatever
  42. * is in the input field.
  43. */
  44. boolean

    appendDigits =

    false

    ;

  45. public

    void

    init(

    Object

    context)

    {
  46. try

    {
  47. theme =

    Resources.openLayered

    (

    "/theme"

    )

    ;
  48. UIManager

    .getInstance

    (

    )

    .setThemeProps

    (

    theme.getTheme

    (

    theme.getThemeResourceNames

    (

    )

    [

    0

    ]

    )

    )

    ;
  49. }

    catch

    (

    IOException

    e)

    {
  50. e.printStackTrace

    (

    )

    ;
  51. }

  52. }

  53. /**
  54. * Clears the current calculation.
  55. */
  56. private

    void

    clear(

    )

    {
  57. inputField.setText

    (

    String

    .valueOf

    (

    0

    )

    )

    ;
  58. op =

    0

    ;
  59. currVal =

    0

    ;
  60. reset =

    true

    ;
  61. appendDigits =

    false

    ;
  62. }

  63. /**
  64. * Executes the calculation given the most recent operation that
  65. * was pressed and displays the value in the input field.
  66. */
  67. private

    void

    exec(

    )

    {

  68. double

    a =

    Double

    .parseDouble

    (

    inputField.getText

    (

    )

    )

    ;
  69. if

    (

    reset)

    {
  70. currVal =

    a;
  71. return

    ;
  72. }

    else

    {
  73. switch

    (

    op)

    {
  74. case

    '+'

    :

    currVal +=

    a;

    break

    ;
  75. case

    '-'

    :

    currVal -=

    a;

    break

    ;
  76. case

    '*'

    :

    currVal *=

    a;

    break

    ;
  77. case

    '/'

    :

    currVal /=

    a;

    break

    ;
  78. }
  79. }
  80. inputField.setText

    (

    String

    .valueOf

    (

    currVal)

    )

    ;
  81. appendDigits =

    true

    ;

  82. }

  83. /**
  84. * Appends the given digit to the input field.
  85. * @param digit
  86. */
  87. private

    void

    append(

    int

    digit)

    {
  88. String

    s =

    digit >=

    0

    ?

    String

    .valueOf

    (

    digit)

    :

    "."

    ;
  89. if

    (

    appendDigits)

    {
  90. inputField.setText

    (

    inputField.getText

    (

    )

    +

    s)

    ;
  91. }

    else

    {
  92. inputField.setText

    (

    s)

    ;
  93. appendDigits =

    true

    ;
  94. }
  95. }

  96. /**
  97. * Sets the current operation, and executes it.
  98. * @param op
  99. */
  100. private

    void

    setOp(

    char

    op)

    {
  101. exec(

    )

    ;
  102. this

    .op

    =

    op;
  103. this

    .reset

    =

    false

    ;

  104. appendDigits =

    false

    ;

  105. }

  106. public

    void

    start(

    )

    {
  107. if

    (

    current !=

    null

    )

    {
  108. current.show

    (

    )

    ;
  109. return

    ;
  110. }
  111. // Create the calculator form
  112. Form f =

    new

    Form(

    "Calculator"

    )

    ;
  113. f.setLayout

    (

    new

    BorderLayout

    (

    )

    )

    ;

  114. // The input field placed in the north section
  115. inputField =

    new

    TextField

    (

    )

    ;
  116. inputField.getDisabledStyle

    (

    )

    .setAlignment

    (

    Component

    .RIGHT

    )

    ;
  117. inputField.setEnabled

    (

    false

    )

    ;
  118. inputField.setText

    (

    String

    .valueOf

    (

    0

    )

    )

    ;
  119. f.addComponent

    (

    BorderLayout

    .NORTH

    , inputField)

    ;


  120. // We place the numbers in a GridLayout with 4 rows and 3 columns.

  121. Container

    numbers =

    new

    Container

    (

    )

    ;
  122. numbers.setLayout

    (

    new

    GridLayout

    (

    4

    ,3

    )

    )

    ;

  123. // We'll set up the digits as integers. Only non-negative ints
  124. // will result in a button being placed for it.
  125. int

    digits[

    ]

    =

    {

    1

    ,2

    ,3

    ,4

    ,5

    ,6

    ,7

    ,8

    ,9

    ,-

    1

    ,0

    }

    ;
  126. for

    (

    int

    i=

    0

    ;

    i<

    digits.length

    ;

    i++

    )

    {
  127. int

    digit =

    digits[

    i]

    ;
  128. if

    (

    digit >=

    0

    )

    {
  129. Button

    digitButton =

    new

    Button

    (

    String

    .valueOf

    (

    digit)

    )

    ;

  130. // Add action listener to digit button which appends
  131. // the current digit to the input.
  132. digitButton.addActionListener

    (

    (

    e)

    ->

    {

    append(

    digit)

    ;

    }

    )

    ;
  133. numbers.addComponent

    (

    digitButton)

    ;
  134. }

    else

    {
  135. // If the digit was negative, we just want an empty space in the
  136. // grid.
  137. Container

    c =

    new

    Container

    (

    )

    ;
  138. numbers.addComponent

    (

    c)

    ;
  139. }
  140. }

  141. // Add a decimal button to the last position in the grid.
  142. Button

    decimalButton =

    new

    Button

    (

    "."

    )

    ;

  143. // We adopt convention that appending -1 results in decimal
  144. // being placed in the input field.
  145. decimalButton.addActionListener

    (

    (

    e)

    ->

    {

    append(

    -

    1

    )

    ;

    }

    )

    ;
  146. numbers.addComponent

    (

    decimalButton)

    ;

  147. f.addComponent

    (

    BorderLayout

    .CENTER

    , numbers)

    ;

  148. // Set up container for the command buttons (+, -, *, etc..)
  149. // We will place this in the east section in a grid layout.
  150. Container

    commands =

    new

    Container

    (

    )

    ;
  151. commands.setPreferredW

    (

    Display.getInstance

    (

    )

    .getDisplayWidth

    (

    )

    /

    4

    )

    ;
  152. commands.setLayout

    (

    new

    GridLayout

    (

    4

    ,1

    )

    )

    ;
  153. Button

    clear =

    new

    Button

    (

    "C"

    )

    ;
  154. clear.addActionListener

    (

    (

    e)

    ->

    {

    clear(

    )

    ;

    }

    )

    ;
  155. commands.addComponent

    (

    clear)

    ;

  156. Button

    plus =

    new

    Button

    (

    "+"

    )

    ;
  157. plus.addActionListener

    (

    (

    e)

    ->

    {

    setOp(

    '+'

    )

    ;

    }

    )

    ;
  158. commands.addComponent

    (

    plus)

    ;

  159. Button

    minus =

    new

    Button

    (

    "-"

    )

    ;
  160. minus.addActionListener

    (

    (

    e)

    ->

    {

    setOp(

    '-'

    )

    ;

    }

    )

    ;
  161. commands.addComponent

    (

    minus)

    ;

  162. Button

    mult =

    new

    Button

    (

    "x"

    )

    ;
  163. mult.addActionListener

    (

    (

    e)

    ->

    {

    setOp(

    '*'

    )

    ;

    }

    )

    ;
  164. commands.addComponent

    (

    mult)

    ;

  165. Button

    div =

    new

    Button

    (

    "/"

    )

    ;
  166. div.addActionListener

    (

    (

    e)

    ->

    {

    setOp(

    '/'

    )

    ;

    }

    )

    ;
  167. commands.addComponent

    (

    div)

    ;

  168. Button

    eq =

    new

    Button

    (

    "="

    )

    ;
  169. eq.addActionListener

    (

    (

    e)

    ->

    {

    setOp(

    '='

    )

    ;

    }

    )

    ;
  170. commands.addComponent

    (

    eq)

    ;


  171. f.addComponent

    (

    BorderLayout

    .EAST

    , commands)

    ;


  172. // Show the form.
  173. f.show

    (

    )

    ;


  174. }

  175. public

    void

    stop(

    )

    {
  176. current =

    Display.getInstance

    (

    )

    .getCurrent

    (

    )

    ;
  177. }

  178. public

    void

    destroy(

    )

    {
  179. }

  180. }

To run/build this, you just need Eclipse, NetBeans, or Idea IntelliJ with the free NetBeans Codename One plugin installed.

I have included the full Netbeans project in the ZIP file attachment so you should be able to download it and run/build it directly in NetBeans.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top