Simple Calculator - Android

View: 118    Dowload: 0   Comment: 0   Post by: daii   Category: Mobile applications   Fields: Game - Entertainment

Download  simple-calculator-android.zip (928.74 KB)

You need to Sign In to download the file simple-calculator-android.zip
If you do not have an account then Sign up for free here

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="<a href="http://schemas.android.com/apk/res/android"
  4. " rel="nofollow">http://schemas.android.com/apk/res/android"
  5. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  6. " rel="nofollow">http://schemas.android.com/tools"
  7. </a> android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. tools:context="com.razormist.simplecalculator.MainActivity">
  10.  
  11. <TextView
  12. android:id="@+id/tv_info"
  13. android:layout_height="wrap_content"
  14. android:layout_width="match_parent"
  15. android:layout_marginBottom="30dp"
  16. android:textSize="30sp" />
  17.  
  18. <EditText
  19. android:id="@+id/et_edit"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@+id/tv_info"
  23. android:enabled="false"
  24. android:gravity="bottom"
  25. android:lines="2"
  26. android:maxLines="2"
  27. android:textSize="30sp" />
  28.  
  29. android:id="@+id/btn_seven"
  30. android:layout_height="wrap_content"
  31. android:layout_width="wrap_content"
  32. android:layout_below="@+id/et_edit"
  33. android:text="7"
  34. android:textSize="20sp"
  35. android:layout_marginLeft="15dp"/>
  36.  
  37. android:id="@+id/btn_eight"
  38. android:layout_height="wrap_content"
  39. android:layout_width="wrap_content"
  40. android:layout_below="@+id/et_edit"
  41. android:layout_toRightOf="@+id/btn_seven"
  42. android:text="8"
  43. android:textSize="20sp"/>
  44.  
  45. android:id="@+id/btn_nine"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_below="@+id/et_edit"
  49. android:layout_toRightOf="@+id/btn_eight"
  50. android:text="9"
  51. android:textSize="20sp"/>
  52.  
  53. android:id="@+id/btn_four"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_below="@+id/btn_seven"
  57. android:text="4"
  58. android:textSize="20sp"
  59. android:layout_marginLeft="15dp"/>
  60.  
  61. android:id="@+id/btn_five"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_below="@+id/btn_eight"
  65. android:text="5"
  66. android:layout_toRightOf="@+id/btn_four"
  67. android:textSize="20sp"/>
  68.  
  69. android:id="@+id/btn_six"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_below="@+id/btn_nine"
  73. android:text="6"
  74. android:layout_toRightOf="@+id/btn_five"
  75. android:textSize="20sp"/>
  76.  
  77. android:id="@+id/btn_one"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. android:layout_below="@+id/btn_four"
  81. android:text="1"
  82. android:textSize="20sp"
  83. android:layout_marginLeft="15dp"/>
  84.  
  85. android:id="@+id/btn_two"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:layout_below="@+id/btn_five"
  89. android:text="2"
  90. android:layout_toRightOf="@+id/btn_one"
  91. android:textSize="20sp"/>
  92.  
  93. android:id="@+id/btn_three"
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content"
  96. android:layout_below="@+id/btn_six"
  97. android:text="3"
  98. android:layout_toRightOf="@+id/btn_two"
  99. android:textSize="20sp"/>
  100.  
  101. android:id="@+id/btn_dot"
  102. android:layout_width="wrap_content"
  103. android:layout_height="wrap_content"
  104. android:layout_below="@+id/btn_one"
  105. android:text="."
  106. android:textSize="20sp"
  107. android:layout_marginLeft="15dp"/>
  108.  
  109. android:id="@+id/btn_zero"
  110. android:layout_width="wrap_content"
  111. android:layout_height="wrap_content"
  112. android:layout_below="@+id/btn_two"
  113. android:layout_toRightOf="@+id/btn_dot"
  114. android:text="0"
  115. android:textSize="20sp"/>
  116.  
  117. android:id="@+id/btn_equals"
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"
  120. android:layout_below="@+id/btn_three"
  121. android:layout_toRightOf="@+id/btn_zero"
  122. android:text="="
  123. android:textSize="20sp"/>
  124.  
  125. android:id="@+id/btn_divide"
  126. android:layout_width="wrap_content"
  127. android:layout_height="wrap_content"
  128. android:layout_below="@+id/et_edit"
  129. android:layout_toRightOf="@+id/btn_nine"
  130. android:text="/"
  131. android:textSize="20sp"/>
  132.  
  133. android:id="@+id/btn_multiply"
  134. android:layout_width="wrap_content"
  135. android:layout_height="wrap_content"
  136. android:layout_below="@+id/btn_divide"
  137. android:layout_toRightOf="@+id/btn_six"
  138. android:text="*"
  139. android:textSize="20sp"/>
  140.  
  141. android:id="@+id/btn_add"
  142. android:layout_width="wrap_content"
  143. android:layout_height="wrap_content"
  144. android:layout_below="@+id/btn_multiply"
  145. android:layout_toRightOf="@+id/btn_three"
  146. android:text="+"
  147. android:textSize="20sp"/>
  148.  
  149. android:id="@+id/btn_minus"
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:layout_below="@+id/btn_add"
  153. android:layout_toRightOf="@+id/btn_equals"
  154. android:text="-"
  155. android:textSize="20sp"/>
  156.  
  157. android:id="@+id/btn_clear"
  158. android:layout_width="match_parent"
  159. android:layout_height="wrap_content"
  160. android:layout_below="@+id/btn_minus"
  161. android:text="CLEAR"
  162. android:textSize="20sp"/>
  163.  
  164. </RelativeLayout>
  165. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplecalculator">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </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!!!

Simple Calculator - Android

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.....

Posted on 12-03-2018 

Comment:

To comment you must be logged in members.

Files with category

  • 8-bit LED Binary Counter

    8-bit LED Binary Counter

    View: 24    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Other

    This article offers a simple implementation of the binary counter powered by Raspberry PI3 and 8 LEDs. It is very similar to what was done before for Arduino board in the previous article Connecting an Arduino to a Breadboard to Light Up LEDs. In...

  • Chat App

    Chat App

    View: 36    Download: 1   Comment: 0

    Category: Android     Fields: none

    This is a Chat App for Android mobile. You can make separate chat with you partner. I'm using Mysql Database and Php code and All APIs are develop on PHP (REST APIs). My whatsApp:+918285672453: Email ID: nirbhayiise@gmail.com

  • Tower Defense 3D

    Tower Defense 3D

    View: 25    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    The goal in this game is to protect your territory against the attacking enemies. Place defensive structures on or along their path of attack to prevent them reaching your base. Strategic choice and positioning of defensive elements is an essential...

  • Flap the Bird

    Flap the Bird

    View: 22    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tap your screen to make the bird fly. While you are flying there are destruction that will stop you. Avoid hitting the floor and flying to high this will deduct your life. You can collect coins and life along the way. For more mobile games and...

  • Bad Santa

    Bad Santa

    View: 28    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This is a simple shooting game where the Bad Santa is shooting the bad snowman and reindeer. You can collect coins and life along the way. For more mobile games and tutorials you can visit this site.

  • Flappy Halloween Game

    Flappy Halloween Game

    View: 24    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Flappy Halloween is a simple game, tap the screen to move the player. Shoot the pumpkin in the ring to gain score. Avoid hitting the flying destructions and falling from the ground. Since the Halloween is coming this game is really perfect to play...

  • Tilt Ball Game

    Tilt Ball Game

    View: 25    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This game is simple you need to tilt your device to make the ball move in your own desired direction. Reach the finish line before the time ends and move to the next level.

  • Sky Rise 3D Game

    Sky Rise 3D Game

    View: 43    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Stack up the blocks as high as you can to build a tower. Align the blocks on top of each other. If you manage to place the block exactly on the one before it, it will not lose size. Sloppy placement on the other hand will cut off the blocks and...

 
File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd