Simple Calculator - Android

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

0 point/1 review File has been tested

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

Simple Calculator - Android Posted on 12-03-2018  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..... 5/10 74

Comment:

To comment you must be logged in members.

Files with category

  • Beautiful and Simple Todo list iOS App

    Beautiful and Simple Todo list iOS App

    View: 15    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    TodoList is an iOS app for making todo lists. It uses core data to save the notes. The app is an excellent starting point to start learning iOS Application Development using swift and getting familiar with Model View Controller architecture. Liked...

  • E - Commerce

    E - Commerce

    View: 16    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    If you’re in a hurry, then E-Commerce App is the best option for you. Late delivery, cold food, no change, low quantity, or same old Dal or Pizza - we wanted to solve this madness for you. So we built a whole new App for you that is a single point to...

  • iSignUp - iOS 6 & Parse

    iSignUp - iOS 6 & Parse

    View: 18    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Basic iOS 6 App created using Objective C & Parse as its Primary data storage..... Parse - ( www.parse.com ) *Features 1.basic C.R.U.D. Process 2.pushing from 1 UIViewController to other UIViewController 3.Populating/Fetching UITableView using...

  • UIAlertViews - iOS

    UIAlertViews - iOS

    View: 13    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    The UIAlertView class displays a blue popup box on the screen and the UIActionSheet causes a panel to slide up from the bottom of the screen.

  • iPhone with MapKit and the MKMapView Class

    iPhone with MapKit and the MKMapView Class

    View: 357    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    The MapKit Framework is based on the Apple Maps data and APIs and provides iPhone developers with a simple mechanism for integrating detailed and interactive mapping capabilities into any application. The core element of the MapKit framework from...

  • Upturn Task Reminder app

    Upturn Task Reminder app

    View: 27    Download: 1   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    Time management techniques say that it's better to keep track of tasks using a reminder system rather than your memory. Owing to intensive agenda, working person may have dozens of works and others tasks that should be done and it is normal that even...

  • Instagram Clone App (Android)

    Instagram Clone App (Android)

    View: 103    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This is an Instagram Clone app for android phone. More information contact on: whatsapp :+918285672453 skype: nirbhay.success mail: nirbhayiise@gmail.com

  • Text To Speech Application in Android Eclipse (Pronunciation Made Easy App)

    Text To Speech Application in Android Eclipse (Pronunciation Made Easy App)

    View: 242    Download: 0   Comment: 0

    Category: Mobile applications     Fields: Game - Entertainment

    This Android application entitled Pronunciation Made Easy was programmed by my younger brother as his project for their mobile application development subject. This application aims to help individuals who have difficulties or questions on how to...

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