Introduction

Android is one of the most popular operating systems for mobile. In this article, I will show you how to create a Native Calculator Android application using Android Studio.
Requirements
Steps to be followed
The following steps are required to create a native Calculator Android application using Android Studio and I have included the source code below.
Step 1
Open Android Studio and start a new Android Studio Project.
Android Application
Step 2
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
Android Application
Now, select the version of Android and select the target Android devices.
Android Application
Step 3
Now, add the activity and click the "Next" button.
Android Application
Add Activity name and click the "Finish" button.
Android Application
Step 4
Go to activity_main.xml, This XML file contains the designing code for an Android app in the activity_main.xml.
Android Application
The XML code is given below.
  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:layout_margin="20dp">
  7. <LinearLayout
  8. android:id="@+id/linearLayout1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_margin="20dp">
  12. <EditText
  13. android:id="@+id/editText1"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:inputType="numberDecimal"
  18. android:textSize="20sp" />
  19. <EditText
  20. android:id="@+id/editText2"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:inputType="numberDecimal"
  25. android:textSize="20sp" />
  26. </LinearLayout>
  27. <LinearLayout
  28. android:id="@+id/linearLayout2"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_margin="20dp">
  32. <Button
  33. android:id="@+id/Add"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="1"
  37. android:text="+"
  38. android:textSize="30sp"/>
  39. <Button
  40. android:id="@+id/Sub"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. android:layout_weight="1"
  44. android:text="-"
  45. android:textSize="30sp"/>
  46. <Button
  47. android:id="@+id/Mul"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:layout_weight="1"
  51. android:text="*"
  52. android:textSize="30sp"/>
  53. <Button
  54. android:id="@+id/Div"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:layout_weight="1"
  58. android:text="/"
  59. android:textSize="30sp"/>
  60. </LinearLayout>
  61. <TextView
  62. android:id="@+id/textView"
  63. android:layout_width="match_parent"
  64. android:layout_height="wrap_content"
  65. android:layout_marginTop="50dp"
  66. android:text="The Answer is"
  67. android:textSize="30sp"
  68. android:gravity="center"/>
  69. </LinearLayout>
Step 5
Go to Main Activity.java, This Java program is the back-end language for Android. Add the following code.
  1. package com.example.hpworld.nativecalculator;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. public class MainActivity extends AppCompatActivity implements OnClickListener
  11. {
  12. //Defining the Views
  13. EditText Num1;
  14. EditText Num2;
  15. Button Add;
  16. Button Sub;
  17. Button Mul;
  18. Button Div;
  19. TextView Result;
  20. @Override
  21. public void onCreate(Bundle savedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. //Referring the Views
  26. Num1 = (EditText) findViewById(R.id.editText1);
  27. Num2 = (EditText) findViewById(R.id.editText2);
  28. Add = (Button) findViewById(R.id.Add);
  29. Sub = (Button) findViewById(R.id.Sub);
  30. Mul = (Button) findViewById(R.id.Mul);
  31. Div = (Button) findViewById(R.id.Div);
  32. Result = (TextView) findViewById(R.id.textView);
  33. // set a listener
  34. Add.setOnClickListener(this);
  35. Sub.setOnClickListener(this);
  36. Mul.setOnClickListener(this);
  37. Div.setOnClickListener(this);
  38. }
  39. @Override
  40. public void onClick (View v)
  41. {
  42. float num1 = 0;
  43. float num2 = 0;
  44. float result = 0;
  45. String oper = "";
  46. // check if the fields are empty
  47. if (TextUtils.isEmpty(Num1.getText().toString()) || TextUtils.isEmpty(Num2.getText().toString()))
  48. return;
  49. // read EditText and fill variables with numbers
  50. num1 = Float.parseFloat(Num1.getText().toString());
  51. num2 = Float.parseFloat(Num2.getText().toString());
  52. // defines the button that has been clicked and performs the corresponding operation
  53. // write operation into oper, we will use it later for output
  54. switch (v.getId())
  55. {
  56. case R.id.Add:
  57. oper = "+";
  58. result = num1 + num2;
  59. break;
  60. case R.id.Sub:
  61. oper = "-";
  62. result = num1 - num2;
  63. break;
  64. case R.id.Mul:
  65. oper = "*";
  66. result = num1 * num2;
  67. break;
  68. case R.id.Div:
  69. oper = "/";
  70. result = num1 / num2;
  71. break;
  72. default:
  73. break;
  74. }
  75. // form the output line
  76. Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
  77. }
  78. }
Step 6
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
Android Application
Step 7
Then, click the Run button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.
Android Application

Conclusion

We have successfully created a Calculator app for Android using Android Studio.
Android Application
Result
Addition Operation
Android Application
Subtraction Operation
Android Application
Multiplication Operation
Android Application
Division Operation
Android Application