How To Create Calculator App For Android

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
  • Android Studio.
  • Little bit XML and JAVA knowledge.
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.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.TextUtils;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11. public class CalcActivity extends Activity implements  
  12.         OnClickListener  
  13. {  
  14.     EditText input1;  
  15.     EditText input2;  
  16.     Button addition;  
  17.     Button subtraction;  
  18.     Button multiplication;  
  19.     Button division;  
  20.     TextView tvResult;  
  21.     String oper = "";  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState)  
  24.     {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_calc);  
  27.         input1 = (EditText) findViewById(R.id.etNum1);  
  28.         input2 = (EditText) findViewById(R.id.etNum2);  
  29.         addition = (Button) findViewById(R.id.btnAdd);  
  30.         subtraction = (Button) findViewById(R.id.btnSub);  
  31.         multiplication = (Button) findViewById(R.id.btnMult);  
  32.         division = (Button) findViewById(R.id.btnDiv);  
  33.         tvResult = (TextView) findViewById(R.id.tvResult);  
  34.         // set a listener  
  35.         addition.setOnClickListener(this);  
  36.         subtraction.setOnClickListener(this);  
  37.         multiplication.setOnClickListener(this);  
  38.         division.setOnClickListener(this);  
  39.     }  
  40.     @Override  
  41.     public void onClick(View v)  
  42.     {  
  43.         // TODO Auto-generated method stub  
  44.         float num1 = 0;  
  45.         float num2 = 0;  
  46.         float result = 0;  
  47.         // check if the fields are empty  
  48.         if (TextUtils.isEmpty(input1.getText().toString())  
  49.                 || TextUtils.isEmpty(input2.getText().toString()))  
  50.         {  
  51.             return;  
  52.         }  
  53.         // read EditText and fill variables with numbers  
  54.         num1 = Float.parseFloat(input1.getText().toString());  
  55.         num2 = Float.parseFloat(input2.getText().toString());  
  56.   
  57.         // defines the button that has been clicked and performs the corresponding operation  
  58.         // write operation into oper, we will use it later for output  
  59.         switch (v.getId())  
  60.         {  
  61.             case R.id.btnAdd:  
  62.                 oper = "+";  
  63.                 result = num1 + num2;  
  64.                 break;  
  65.             case R.id.btnSub:  
  66.                 oper = "-";  
  67.                 result = num1 - num2;  
  68.                 break;  
  69.             case R.id.btnMult:  
  70.                 oper = "*";  
  71.                 result = num1 * num2;  
  72.                 break;  
  73.             case R.id.btnDiv:  
  74.                 oper = "/";  
  75.                 result = num1 / num2;  
  76.                 break;  
  77.             default:  
  78.                 break;  
  79.         }  
  80.   
  81.         // form the output line  
  82.         tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);  
  83.     }  
  84. }    
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.


Similar Articles