Introduction

Ninety percent of people today use Android smartphones. In our day to day life, we perform mathematical operations with the help of a calculator. Thus, I will show you how to create a Calculator App for Android, using Android Studio. Android is the Kernel-based operating system. It allows the user, to modify GUI components and the source code.
Requirements
Download link (Android Studio): https://developer.android.com/studio/index.html.

Steps to be followed are given below

Carefully follow my steps to create a Calculator App for an Android and I have included the source code given below.
Step 1
Open an Android Studio. Start the new project.
android
Step 2
Put the Application name and company domain. If you wish to use C++ for coding the project, mark the Include C++ support, followed by clicking Next.
android
Step 3
Select the Android minimum SDK. Afterward, you chose the minimum SDK and it will show the approximate percentage of the people, who use SDK, followed by clicking Next.
android
Step 4
Choose the basic activity, followed by clicking Next.
android
Step 5
Put the activity name and layout name. Android Studio basically takes a Java class name, which is provided by you in the activity name
android
Step 6
Go to activity_calc.xml, followed by clicking the bottom text. This XML file contains the designing code for an Android app in the activity_calc.xml; copy and paste the code given below.
activity_calc.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/linearLayout1"
  11. android:layout_marginLeft="10pt"
  12. android:layout_marginRight="10pt"
  13. android:layout_marginTop="3pt">
  14. <EditText
  15. android:layout_weight="1"
  16. android:layout_height="wrap_content"
  17. android:layout_marginRight="5pt"
  18. android:id="@+id/etNum1"
  19. android:layout_width="match_parent"
  20. android:inputType="numberDecimal">
  21. </EditText>
  22. <EditText
  23. android:layout_height="wrap_content"
  24. android:layout_weight="1"
  25. android:layout_marginLeft="5pt"
  26. android:id="@+id/etNum2"
  27. android:layout_width="match_parent"
  28. android:inputType="numberDecimal">
  29. </EditText>
  30. </LinearLayout>
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:id="@+id/linearLayout2"
  35. android:layout_marginTop="3pt"
  36. android:layout_marginLeft="5pt"
  37. android:layout_marginRight="5pt">
  38. <Button
  39. android:layout_height="wrap_content"
  40. android:layout_width="match_parent"
  41. android:layout_weight="1"
  42. android:text="+"
  43. android:textSize="15pt"
  44. android:id="@+id/btnAdd">
  45. </Button>
  46. <Button
  47. android:layout_height="wrap_content"
  48. android:layout_width="match_parent"
  49. android:layout_weight="1"
  50. android:text="-"
  51. android:textSize="15pt"
  52. android:id="@+id/btnSub">
  53. </Button>
  54. <Button
  55. android:layout_height="wrap_content"
  56. android:layout_width="match_parent"
  57. android:layout_weight="1"
  58. android:text="*"
  59. android:textSize="15pt"
  60. android:id="@+id/btnMult">
  61. </Button>
  62. <Button
  63. android:layout_height="wrap_content"
  64. android:layout_width="match_parent"
  65. android:layout_weight="1"
  66. android:text="/"
  67. android:textSize="15pt"
  68. android:id="@+id/btnDiv">
  69. </Button>
  70. </LinearLayout>
  71. <TextView
  72. android:layout_height="wrap_content"
  73. android:layout_width="match_parent"
  74. android:layout_marginLeft="5pt"
  75. android:layout_marginRight="5pt"
  76. android:textSize="12pt"
  77. android:layout_marginTop="3pt"
  78. android:id="@+id/tvResult"
  79. android:gravity="center_horizontal">
  80. </TextView>
  81. </LinearLayout>
android
Step 8
In CalcActivity.java, copy and paste the code of Java programming given below, which is the backend language for an Android. Do not replace your package name, else an app will not run. The code given below contains my package name.
CalcActivity.java code
  1. package ganeshannt.calc;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  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 CalcActivity extends Activity implements
  11. OnClickListener
  12. {
  13. EditText input1;
  14. EditText input2;
  15. Button addition;
  16. Button subtraction;
  17. Button multiplication;
  18. Button division;
  19. TextView tvResult;
  20. String oper = "";
  21. @Override
  22. public void onCreate(Bundle savedInstanceState)
  23. {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_calc);
  26. input1 = (EditText) findViewById(R.id.etNum1);
  27. input2 = (EditText) findViewById(R.id.etNum2);
  28. addition = (Button) findViewById(R.id.btnAdd);
  29. subtraction = (Button) findViewById(R.id.btnSub);
  30. multiplication = (Button) findViewById(R.id.btnMult);
  31. division = (Button) findViewById(R.id.btnDiv);
  32. tvResult = (TextView) findViewById(R.id.tvResult);
  33. // set a listener
  34. addition.setOnClickListener(this);
  35. subtraction.setOnClickListener(this);
  36. multiplication.setOnClickListener(this);
  37. division.setOnClickListener(this);
  38. }
  39. @Override
  40. public void onClick(View v)
  41. {
  42. // TODO Auto-generated method stub
  43. float num1 = 0;
  44. float num2 = 0;
  45. float result = 0;
  46. // check if the fields are empty
  47. if (TextUtils.isEmpty(input1.getText().toString())
  48. || TextUtils.isEmpty(input2.getText().toString()))
  49. {
  50. return;
  51. }
  52. // read EditText and fill variables with numbers
  53. num1 = Float.parseFloat(input1.getText().toString());
  54. num2 = Float.parseFloat(input2.getText().toString());
  55. // defines the button that has been clicked and performs the corresponding operation
  56. // write operation into oper, we will use it later for output
  57. switch (v.getId())
  58. {
  59. case R.id.btnAdd:
  60. oper = "+";
  61. result = num1 + num2;
  62. break;
  63. case R.id.btnSub:
  64. oper = "-";
  65. result = num1 - num2;
  66. break;
  67. case R.id.btnMult:
  68. oper = "*";
  69. result = num1 * num2;
  70. break;
  71. case R.id.btnDiv:
  72. oper = "/";
  73. result = num1 / num2;
  74. break;
  75. default:
  76. break;
  77. }
  78. // form the output line
  79. tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
  80. }
  81. }
Step 9
This is our user interface of the Application. Click Make project option.
android
Step 10
Run the Application, followed by choosing the virtual machine. Click OK.
android
Deliverables
Here, we successfully created a Calculator app for an Android, using an Android Studio Application, which was created and executed.
android
Addition operation
android
Subtraction operation
android
Divide operation
android
Multiplication operation
android
If you have any doubts, just comment below.