Skip to main content

Android - Simple Calculator

Operating System

In this tutorial we will try to create a Simple Calculator Using Android. Android is basically a piece of software which allows your hardware to function. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the activity_main.xml and click text to view the script. Then copy and paste the code below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <layout>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplecalculator.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/tv_info"
  11. android:layout_height="wrap_content"
  12. android:layout_width="match_parent"
  13. android:layout_marginBottom="30dp"
  14. android:textSize="30sp" />
  15.  
  16. <EditText
  17. android:id="@+id/et_edit"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@+id/tv_info"
  21. android:enabled="false"
  22. android:gravity="bottom"
  23. android:lines="2"
  24. android:maxLines="2"
  25. android:textSize="30sp" />
  26.  
  27. android:id="@+id/btn_seven"
  28. android:layout_height="wrap_content"
  29. android:layout_width="wrap_content"
  30. android:layout_below="@+id/et_edit"
  31. android:text="7"
  32. android:textSize="20sp"
  33. android:layout_marginLeft="15dp"/>
  34.  
  35. android:id="@+id/btn_eight"
  36. android:layout_height="wrap_content"
  37. android:layout_width="wrap_content"
  38. android:layout_below="@+id/et_edit"
  39. android:layout_toRightOf="@+id/btn_seven"
  40. android:text="8"
  41. android:textSize="20sp"/>
  42.  
  43. android:id="@+id/btn_nine"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_below="@+id/et_edit"
  47. android:layout_toRightOf="@+id/btn_eight"
  48. android:text="9"
  49. android:textSize="20sp"/>
  50.  
  51. android:id="@+id/btn_four"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_below="@+id/btn_seven"
  55. android:text="4"
  56. android:textSize="20sp"
  57. android:layout_marginLeft="15dp"/>
  58.  
  59. android:id="@+id/btn_five"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_below="@+id/btn_eight"
  63. android:text="5"
  64. android:layout_toRightOf="@+id/btn_four"
  65. android:textSize="20sp"/>
  66.  
  67. android:id="@+id/btn_six"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_below="@+id/btn_nine"
  71. android:text="6"
  72. android:layout_toRightOf="@+id/btn_five"
  73. android:textSize="20sp"/>
  74.  
  75. android:id="@+id/btn_one"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_below="@+id/btn_four"
  79. android:text="1"
  80. android:textSize="20sp"
  81. android:layout_marginLeft="15dp"/>
  82.  
  83. android:id="@+id/btn_two"
  84. android:layout_width="wrap_content"
  85. android:layout_height="wrap_content"
  86. android:layout_below="@+id/btn_five"
  87. android:text="2"
  88. android:layout_toRightOf="@+id/btn_one"
  89. android:textSize="20sp"/>
  90.  
  91. android:id="@+id/btn_three"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:layout_below="@+id/btn_six"
  95. android:text="3"
  96. android:layout_toRightOf="@+id/btn_two"
  97. android:textSize="20sp"/>
  98.  
  99. android:id="@+id/btn_dot"
  100. android:layout_width="wrap_content"
  101. android:layout_height="wrap_content"
  102. android:layout_below="@+id/btn_one"
  103. android:text="."
  104. android:textSize="20sp"
  105. android:layout_marginLeft="15dp"/>
  106.  
  107. android:id="@+id/btn_zero"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:layout_below="@+id/btn_two"
  111. android:layout_toRightOf="@+id/btn_dot"
  112. android:text="0"
  113. android:textSize="20sp"/>
  114.  
  115. android:id="@+id/btn_equals"
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content"
  118. android:layout_below="@+id/btn_three"
  119. android:layout_toRightOf="@+id/btn_zero"
  120. android:text="="
  121. android:textSize="20sp"/>
  122.  
  123. android:id="@+id/btn_divide"
  124. android:layout_width="wrap_content"
  125. android:layout_height="wrap_content"
  126. android:layout_below="@+id/et_edit"
  127. android:layout_toRightOf="@+id/btn_nine"
  128. android:text="/"
  129. android:textSize="20sp"/>
  130.  
  131. android:id="@+id/btn_multiply"
  132. android:layout_width="wrap_content"
  133. android:layout_height="wrap_content"
  134. android:layout_below="@+id/btn_divide"
  135. android:layout_toRightOf="@+id/btn_six"
  136. android:text="*"
  137. android:textSize="20sp"/>
  138.  
  139. android:id="@+id/btn_add"
  140. android:layout_width="wrap_content"
  141. android:layout_height="wrap_content"
  142. android:layout_below="@+id/btn_multiply"
  143. android:layout_toRightOf="@+id/btn_three"
  144. android:text="+"
  145. android:textSize="20sp"/>
  146.  
  147. android:id="@+id/btn_minus"
  148. android:layout_width="wrap_content"
  149. android:layout_height="wrap_content"
  150. android:layout_below="@+id/btn_add"
  151. android:layout_toRightOf="@+id/btn_equals"
  152. android:text="-"
  153. android:textSize="20sp"/>
  154.  
  155. android:id="@+id/btn_clear"
  156. android:layout_width="match_parent"
  157. android:layout_height="wrap_content"
  158. android:layout_below="@+id/btn_minus"
  159. android:text="CLEAR"
  160. android:textSize="20sp"/>
  161.  
  162. </RelativeLayout>
  163. </layout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplecalculator">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

Configuring The Module

The module provide a container for your app's source code, resource files, and app level settings. Android Studio automatically creates module directories, a default build.gradle file appropriate for the device type. We will add some reference to make a certain script works. Firs locate the build.gradle and open, then add this line inside of it.
  1. dataBinding.enabled = true
Full Code:
  1. apply plugin: 'com.android.application'
  2.  
  3. android {
  4. compileSdkVersion 25
  5. buildToolsVersion "25.0.3"
  6. dataBinding.enabled = true
  7. defaultConfig {
  8. applicationId "com.razormist.simplecalculator"
  9. minSdkVersion 15
  10. targetSdkVersion 25
  11. versionCode 1
  12. versionName "1.0"
  13. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  14. }
  15. buildTypes {
  16. release {
  17. minifyEnabled false
  18. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  19. }
  20. }
  21. }
  22.  
  23. dependencies {
  24. compile fileTree(dir: 'libs', include: ['*.jar'])
  25. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  26. exclude group: 'com.android.support', module: 'support-annotations'
  27. })
  28. compile 'com.android.support:appcompat-v7:25.3.1'
  29. compile 'com.android.support.constraint:constraint-layout:1.0.2'
  30. testCompile 'junit:junit:4.12'
  31. }

The Main Function

This code contains the main function of the application. This code will calculate the numbers that you enter depend on what equation you use. To start with first locate your java file and open it, then write these some important variables inside the MainActivity class.
  1. private ActivityMainBinding binding;
  2.  
  3.  
  4. private Double val1 = Double.NaN;
  5. private Double val2;
  6.  
  7. private static final char ADDITION = '+';
  8. private static final char SUBTRACTION = '-';
  9. private static final char MULTIPLICATION = '*';
  10. private static final char DIVISION = '/';
  11.  
  12. private char CURRENT_ACTION;
  13.  
  14.  
  15.  
  16. private DecimalFormat decimalFormat;
Then after that write these block of codes inside onCreate method, these will detect the button that you are touching and display it at the same time.
  1. decimalFormat = new DecimalFormat("#.##########");
  2.  
  3. binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
  4.  
  5. binding.btnSeven.setOnClickListener(new View.OnClickListener() {
  6. @Override
  7. public void onClick(View v) {
  8. binding.etEdit.setText(binding.etEdit.getText() + "7");
  9. }
  10. });
  11.  
  12. binding.btnEight.setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View v) {
  15. binding.etEdit.setText(binding.etEdit.getText() + "8");
  16. }
  17. });
  18.  
  19. binding.btnNine.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. binding.etEdit.setText(binding.etEdit.getText() + "9");
  23. }
  24. });
  25.  
  26. binding.btnFour.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. binding.etEdit.setText(binding.etEdit.getText() + "4");
  30. }
  31. });
  32.  
  33. binding.btnFive.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. binding.etEdit.setText(binding.etEdit.getText() + "5");
  37. }
  38. });
  39.  
  40. binding.btnSix.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. binding.etEdit.setText(binding.etEdit.getText() + "6");
  44. }
  45. });
  46.  
  47. binding.btnOne.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View v) {
  50. binding.etEdit.setText(binding.etEdit.getText() + "1");
  51. }
  52. });
  53.  
  54. binding.btnTwo.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. binding.etEdit.setText(binding.etEdit.getText() + "2");
  58. }
  59. });
  60.  
  61. binding.btnThree.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. binding.etEdit.setText(binding.etEdit.getText() + "3");
  65. }
  66. });
  67.  
  68.  
  69. binding.btnDot.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View v) {
  72. binding.etEdit.setText(binding.etEdit.getText() + ".");
  73. }
  74. });
  75.  
  76. binding.btnZero.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. binding.etEdit.setText(binding.etEdit.getText() + "0");
  80. }
  81. });
  82.  
  83. binding.btnEquals.setOnClickListener(new View.OnClickListener() {
  84. @Override
  85. public void onClick(View v) {
  86. Calculate();
  87. binding.tvInfo.setText(binding.tvInfo.getText().toString() + decimalFormat.format(val2) + " = " + decimalFormat.format(val1));
  88. val1 = Double.NaN;
  89. CURRENT_ACTION = '0';
  90. }
  91. });
  92.  
  93. binding.btnDivide.setOnClickListener(new View.OnClickListener() {
  94. @Override
  95. public void onClick(View v) {
  96. Calculate();
  97. CURRENT_ACTION = DIVISION;
  98. binding.tvInfo.setText(decimalFormat.format(val1) + " / ");
  99. binding.etEdit.setText(null);
  100. }
  101. });
  102.  
  103. binding.btnMultiply.setOnClickListener(new View.OnClickListener() {
  104. @Override
  105. public void onClick(View v) {
  106. Calculate();
  107. CURRENT_ACTION = MULTIPLICATION;
  108. binding.tvInfo.setText(decimalFormat.format(val1) + " * ");
  109. binding.etEdit.setText(null);
  110. }
  111. });
  112.  
  113.  
  114. binding.btnAdd.setOnClickListener(new View.OnClickListener() {
  115. @Override
  116. public void onClick(View v) {
  117. Calculate();
  118. CURRENT_ACTION = ADDITION;
  119. binding.tvInfo.setText(decimalFormat.format(val1) + " + ");
  120. binding.etEdit.setText(null);
  121. }
  122. });
  123.  
  124. binding.btnMinus.setOnClickListener(new View.OnClickListener() {
  125. @Override
  126. public void onClick(View v) {
  127. Calculate();
  128. CURRENT_ACTION = SUBTRACTION;
  129. binding.tvInfo.setText(decimalFormat.format(val1) + " - ");
  130. binding.etEdit.setText(null);
  131. }
  132. });
  133.  
  134. binding.btnClear.setOnClickListener(new View.OnClickListener() {
  135. @Override
  136. public void onClick(View v) {
  137. if(binding.etEdit.getText().length() > 0){
  138. CharSequence currentText = binding.etEdit.getText();
  139. binding.etEdit.setText(currentText.subSequence(0, currentText.length() - 1));
  140. }else{
  141. val1 = Double.NaN;
  142. val2 = Double.NaN;
  143. binding.etEdit.setText("");
  144. binding.tvInfo.setText("");
  145. }
  146. }
  147. });
Now that the buttons are working we will now add the calculation on the app. Write these method called Calculation on the Activity class.
  1. private void Calculate(){
  2. if (!Double.isNaN(val1)){
  3. val2 = Double.parseDouble(binding.etEdit.getText().toString());
  4. binding.etEdit.setText(null);
  5.  
  6. switch (CURRENT_ACTION){
  7. case ADDITION:
  8. val1 = this.val1 + val2;
  9. break;
  10. case SUBTRACTION:
  11. val1 = this.val1 - val2;
  12. break;
  13. case MULTIPLICATION:
  14. val1 = this.val1 * val2;
  15. break;
  16. case DIVISION:
  17. val1 = this.val1 / val2;
  18. break;
  19. }
  20. }else{
  21. try{
  22. val1 = Double.parseDouble(binding.etEdit.getText().toString());
  23. }catch (Exception e){}
  24. }
  25. }
Try to run the app and see if it worked. There you have it we create a Simple Calculator using Android. I hope that this tutorial teach how to deal with android, and enhance your programming capabilities to other language. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

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.

Tags

Add new comment