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

Android Calculator

Lokaks

Ranked Climber
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
144
Likes
43
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Operating System

Android

This tutorial is about the process of creating a calculator for android using Eclipse.

A good example of an application for beginner Android developers is the process of creating an own calculator application.

The first step of this project is creating of new Android project with blank activity. Set the name of the project android_calc and keep the name of your Activity activity_main

.
The next step is to set the title of the application: go to the folder res/values, find there file, called strings.xml

and change the string app_name to My calculator

.
Now we can start to work on the layout of the main activity: open activity_main.xml file and remove the whole content of this file.
We will use the TableLayout

for the calculator's screen. In the top will be placed a text field and below - all the buttons with digits and operations button.
Add TableLayout

inside your RelativeLayout

:
  1. <

    TableLayout
  2. xmlns:

    android=

    "http://schemas.android.com/apk/res/android"
  3. xmlns:

    tools=

    "http://schemas.android.com/tools"
  4. android:

    layout_width=

    "fill_parent"
  5. android:

    layout_height=

    "fill_parent"
  6. android:

    layout_alignParentLeft=

    "true"
  7. android:

    layout_alignParentTop=

    "true"
  8. android:

    layout_alignParentRight=

    "true"
  9. android:

    layout_alignParentBottom=

    "true"
  10. tools:

    context=

    "com.example.android_calc.MainActivity"

    >

The calculator will fill the parent. Now we need to add rows to this TableLayout

. The layout will contain 5 rows: 1 for text field and 4 rows for buttons. Add a row to your layout with a TextView

element inside it:
  1. <

    TableRow
  2. android:

    id=

    "@+id/tableRow1"
  3. android:

    layout_width=

    "fill_parent"
  4. android:

    layout_height=

    "wrap_content"

    >

  5. <

    EditText
  6. android:

    id=

    "@+id/calc_screen"
  7. android:

    background=

    "#ffffff"
  8. android:

    layout_weight=

    "1"
  9. android:

    layout_width=

    "0dip"
  10. android:

    layout_height=

    "wrap_content"
  11. android:

    textSize=

    "36sp"
  12. android:

    inputType=

    "none"

    >

  13. </

    EditText>

  14. </

    TableRow>

The next row will contain buttons with digits 7 8 9 and plus operation:
  1. <

    TableRow
  2. android:

    id=

    "@+id/tableRow2"
  3. android:

    layout_width=

    "fill_parent"
  4. android:

    layout_height=

    "wrap_content"

    >

  5. <

    Button


  6. android:

    id=

    "@+id/seven"
  7. android:

    layout_width=

    "0dip"
  8. android:

    layout_weight=

    "0.25"
  9. android:

    layout_height=

    "wrap_content"
  10. android:

    text=

    "7"

    />

  11. <

    Button


  12. android:

    id=

    "@+id/eight"
  13. android:

    layout_width=

    "0dip"
  14. android:

    layout_weight=

    "0.25"
  15. android:

    layout_height=

    "wrap_content"
  16. android:

    text=

    "8"

    />

  17. <

    Button


  18. android:

    id=

    "@+id/nine"
  19. android:

    layout_width=

    "0dip"
  20. android:

    layout_weight=

    "0.25"
  21. android:

    layout_height=

    "wrap_content"
  22. android:

    text=

    "9"

    />

  23. <

    Button


  24. android:

    id=

    "@+id/plus"
  25. android:

    layout_width=

    "0dip"
  26. android:

    layout_weight=

    "0.25"
  27. android:

    layout_height=

    "wrap_content"
  28. android:

    text=

    "+"

    />

  29. </

    TableRow>

The next rows are really similar to this one, so I hope, you can add them by yourself. Of course, the source code is attached to this tutorial, so you can find the details for these rows.
After adding all the rows, your application should have a look like this:
screenshot_2014-08-31-18-17-12.png


The next step is the implementation of the calculation actions:
we need to initialize 16 buttons and link them to the buttons from layout. For this purpose, add buttons and an EditText to your activity class:
  1. Button

    one;
  2. Button

    two;
  3. Button

    three;
  4. Button

    four;
  5. Button

    five;
  6. Button

    six;
  7. Button

    seven;
  8. Button

    eight;
  9. Button

    nine;
  10. Button

    zero;
  11. Button

    plus;
  12. Button

    minus;
  13. Button

    dev;
  14. Button

    mul;
  15. Button

    point;
  16. Button

    equal;
  17. EditText calc;

To work with the buttons and Edit Text we need to initialize them using
  1. findViewById(

    int

    id)

method:
  1. one =

    (

    Button

    )

    findViewById(

    R.id

    .one

    )

    ;
  2. two =

    (

    Button

    )

    findViewById(

    R.id

    .two

    )

    ;
  3. three =

    (

    Button

    )

    findViewById(

    R.id

    .three

    )

    ;
  4. four =

    (

    Button

    )

    findViewById(

    R.id

    .four

    )

    ;
  5. five =

    (

    Button

    )

    findViewById(

    R.id

    .five

    )

    ;
  6. six =

    (

    Button

    )

    findViewById(

    R.id

    .six

    )

    ;
  7. seven =

    (

    Button

    )

    findViewById(

    R.id

    .seven

    )

    ;
  8. eight =

    (

    Button

    )

    findViewById(

    R.id

    .eight

    )

    ;
  9. nine =

    (

    Button

    )

    findViewById(

    R.id

    .nine

    )

    ;
  10. zero =

    (

    Button

    )

    findViewById(

    R.id

    .zero

    )

    ;
  11. plus =

    (

    Button

    )

    findViewById(

    R.id

    .plus

    )

    ;
  12. minus =

    (

    Button

    )

    findViewById(

    R.id

    .minus

    )

    ;
  13. div =

    (

    Button

    )

    findViewById(

    R.id

    .div

    )

    ;
  14. mul =

    (

    Button

    )

    findViewById(

    R.id

    .mul

    )

    ;
  15. point =

    (

    Button

    )

    findViewById(

    R.id

    .point

    )

    ;
  16. equal =

    (

    Button

    )

    findViewById(

    R.id

    .equal

    )

    ;
  17. calc =

    (

    EditText)

    findViewById(

    R.id

    .calc_screen

    )

    ;

The text in the calculator screen is initialized with empty string:
  1. calc.setText

    (

    ""

    )

    ;

We should add on click listeners to all the buttons. MainActivity class will implement OnClickListener interface:
  1. one.setOnClickListener

    (

    this

    )

    ;
  2. two.setOnClickListener

    (

    this

    )

    ;
  3. three.setOnClickListener

    (

    this

    )

    ;
  4. four.setOnClickListener

    (

    this

    )

    ;
  5. five.setOnClickListener

    (

    this

    )

    ;
  6. six.setOnClickListener

    (

    this

    )

    ;
  7. seven.setOnClickListener

    (

    this

    )

    ;
  8. eight.setOnClickListener

    (

    this

    )

    ;
  9. nine.setOnClickListener

    (

    this

    )

    ;
  10. zero.setOnClickListener

    (

    this

    )

    ;

  11. plus.setOnClickListener

    (

    this

    )

    ;
  12. minus.setOnClickListener

    (

    this

    )

    ;
  13. div.setOnClickListener

    (

    this

    )

    ;
  14. mul.setOnClickListener

    (

    this

    )

    ;

  15. equal.setOnClickListener

    (

    this

    )

    ;
  16. point.setOnClickListener

    (

    this

    )

    ;

We also need to clear screen, if equal button was pressed and after this a new value was entered. For this scope add a boolean value clear

and set it to false;
After this we can start to implement public

void

onClick(

View

v)

method.According to the pressed button, the value will be appended to the calc screen or the result will be shown. The first part is to append a symbol to calc screen, if any button, except equal button is pressed:
  1. if

    (

    clear)

    {
  2. calc.setText

    (

    ""

    )

    ;
  3. clear =

    false

    ;
  4. }
  5. int

    id =

    v.getId

    (

    )

    ;
  6. String

    addSymbol=

    ""

    ;
  7. switch

    (

    id)

    {
  8. case

    R.id

    .one

    :
  9. addSymbol =

    "1"

    ;
  10. break

    ;
  11. case

    R.id

    .two

    :
  12. addSymbol =

    "2"

    ;
  13. break

    ;
  14. case

    R.id

    .three

    :
  15. addSymbol =

    "3"

    ;
  16. break

    ;
  17. case

    R.id

    .four

    :
  18. addSymbol =

    "4"

    ;
  19. break

    ;
  20. case

    R.id

    .five

    :
  21. addSymbol =

    "5"

    ;
  22. break

    ;
  23. case

    R.id

    .six

    :
  24. addSymbol =

    "6"

    ;
  25. break

    ;
  26. case

    R.id

    .seven

    :
  27. addSymbol =

    "7"

    ;
  28. break

    ;
  29. case

    R.id

    .eight

    :
  30. addSymbol =

    "8"

    ;
  31. break

    ;
  32. case

    R.id

    .nine

    :
  33. addSymbol =

    "9"

    ;
  34. break

    ;
  35. case

    R.id

    .zero

    :
  36. addSymbol =

    "0"

    ;
  37. break

    ;
  38. case

    R.id

    .plus

    :
  39. addSymbol =

    "+"

    ;
  40. break

    ;
  41. case

    R.id

    .minus

    :
  42. addSymbol =

    "-"

    ;
  43. break

    ;
  44. case

    R.id

    .div

    :
  45. addSymbol =

    "/"

    ;
  46. break

    ;
  47. case

    R.id

    .mul

    :
  48. addSymbol =

    "*"

    ;

Now, let's develop the part with equal button pressed. To evaluate a math expressions I'm using these imports:
  1. import

    javax.script.ScriptEngineManager

    ;
  2. import

    javax.script.ScriptEngine

    ;

For this import you need to download the next jar file and add it to your project:
JAR
Now,you can evaluate a string like a math expression in this way:
  1. case

    R.id

    .equal

    :
  2. ScriptEngineManager mgr =

    new

    ScriptEngineManager(

    )

    ;
  3. ScriptEngine engine =

    mgr.getEngineByName

    (

    "js"

    )

    ;
  4. String

    expres =

    calc.getText

    (

    )

    .toString

    (

    )

    ;
  5. try

    {
  6. calc.setText

    (

    (

    (

    double

    )

    engine.eval

    (

    expres)

    )

    +

    ""

    )

    ;
  7. }

    catch

    (

    Exception

    e)

    {
  8. //do nothing if theexpression is incorrect

  9. }
  10. clear=

    true

    ;
  11. return

    ;

Now, you are having a simple calculator. I hope, this example is helpful for you. The last piece of code can look a little bit strange, but you need to understand it in a way that you simply create an engine, that can evaluate string expressions and return a result.

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.
 

449,193

322,229

322,238

Top