Monday, September 1, 2014

android calculator sample code ေတြEclipse

Eclipse software နဲ႔ android developing လုပ္မယ္
Posted: 31 Aug 2014 07:49 PM PDT
Submitted by: 
Monday, September 1, 2014 - 10:49
Operating System: 
Visitors have accessed this post 31 times.

Screenshot: 
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.  
  6. <EditText
  7. android:id="@+id/calc_screen"
  8. android:background="#ffffff"
  9. android:layout_weight="1"
  10. android:layout_width="0dip"
  11. android:layout_height="wrap_content"
  12. android:textSize="36sp"
  13. android:inputType="none" >
  14.  
  15. </EditText>
  16.  
  17. </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.  
  6. <Button
  7. android:id="@+id/seven"
  8. android:layout_width="0dip"
  9. android:layout_weight="0.25"
  10. android:layout_height="wrap_content"
  11. android:text="7" />
  12.  
  13. <Button
  14. android:id="@+id/eight"
  15. android:layout_width="0dip"
  16. android:layout_weight="0.25"
  17. android:layout_height="wrap_content"
  18. android:text="8" />
  19.  
  20. <Button
  21. android:id="@+id/nine"
  22. android:layout_width="0dip"
  23. android:layout_weight="0.25"
  24. android:layout_height="wrap_content"
  25. android:text="9" />
  26.  
  27. <Button
  28. android:id="@+id/plus"
  29. android:layout_width="0dip"
  30. android:layout_weight="0.25"
  31. android:layout_height="wrap_content"
  32. android:text="+" />
  33.  
  34. </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:
calc
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.  
  12. plus.setOnClickListener(this);
  13. minus.setOnClickListener(this);
  14. div.setOnClickListener(this);
  15. mul.setOnClickListener(this);
  16.  
  17. equal.setOnClickListener(this);
  18. 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 clearand 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. }
  11. clear=true;
  12. 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.

No comments:

Post a Comment