Create Native Calculator Android Application

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.    
  8.     <LinearLayout  
  9.         android:id="@+id/linearLayout1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_margin="20dp">  
  13.    
  14.         <EditText  
  15.             android:id="@+id/editText1"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1"  
  19.             android:inputType="numberDecimal"  
  20.             android:textSize="20sp" />  
  21.    
  22.         <EditText  
  23.             android:id="@+id/editText2"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_weight="1"  
  27.             android:inputType="numberDecimal"  
  28.             android:textSize="20sp" />  
  29.    
  30.     </LinearLayout>  
  31.    
  32.     <LinearLayout  
  33.         android:id="@+id/linearLayout2"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_margin="20dp">  
  37.    
  38.         <Button  
  39.             android:id="@+id/Add"  
  40.             android:layout_width="match_parent"  
  41.             android:layout_height="wrap_content"  
  42.             android:layout_weight="1"  
  43.             android:text="+"  
  44.             android:textSize="30sp"/>  
  45.    
  46.         <Button  
  47.             android:id="@+id/Sub"  
  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.    
  54.         <Button  
  55.             android:id="@+id/Mul"  
  56.             android:layout_width="match_parent"  
  57.             android:layout_height="wrap_content"  
  58.             android:layout_weight="1"  
  59.             android:text="*"  
  60.             android:textSize="30sp"/>  
  61.    
  62.         <Button  
  63.             android:id="@+id/Div"  
  64.             android:layout_width="match_parent"  
  65.             android:layout_height="wrap_content"  
  66.             android:layout_weight="1"  
  67.             android:text="/"  
  68.             android:textSize="30sp"/>  
  69.    
  70.     </LinearLayout>  
  71.    
  72.    
  73.    <TextView  
  74.         android:id="@+id/textView"  
  75.         android:layout_width="match_parent"  
  76.         android:layout_height="wrap_content"  
  77.         android:layout_marginTop="50dp"  
  78.         android:text="The Answer is"  
  79.         android:textSize="30sp"  
  80.         android:gravity="center"/>  
  81.    
  82. </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.    
  11. public class MainActivity extends AppCompatActivity implements OnClickListener  
  12. {  
  13.     //Defining the Views  
  14.     EditText Num1;  
  15.     EditText Num2;  
  16.     Button Add;  
  17.     Button Sub;  
  18.     Button Mul;  
  19.     Button Div;  
  20.     TextView Result;  
  21.    
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState)  
  24.     {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.    
  28.         //Referring the Views  
  29.         Num1 = (EditText) findViewById(R.id.editText1);  
  30.         Num2 = (EditText) findViewById(R.id.editText2);  
  31.         Add = (Button) findViewById(R.id.Add);  
  32.         Sub = (Button) findViewById(R.id.Sub);  
  33.         Mul = (Button) findViewById(R.id.Mul);  
  34.         Div = (Button) findViewById(R.id.Div);  
  35.         Result = (TextView) findViewById(R.id.textView);  
  36.    
  37.         // set a listener  
  38.         Add.setOnClickListener(this);  
  39.         Sub.setOnClickListener(this);  
  40.         Mul.setOnClickListener(this);  
  41.         Div.setOnClickListener(this);  
  42.     }  
  43.        
  44.     @Override  
  45.     public void onClick (View v)  
  46.     {  
  47.            
  48.         float num1 = 0;  
  49.         float num2 = 0;  
  50.         float result = 0;  
  51.         String oper = "";  
  52.    
  53.         // check if the fields are empty  
  54.         if (TextUtils.isEmpty(Num1.getText().toString()) || TextUtils.isEmpty(Num2.getText().toString()))  
  55.                 return;  
  56.    
  57.         // read EditText and fill variables with numbers  
  58.         num1 = Float.parseFloat(Num1.getText().toString());  
  59.         num2 = Float.parseFloat(Num2.getText().toString());  
  60.    
  61.         // defines the button that has been clicked and performs the corresponding operation  
  62.         // write operation into oper, we will use it later for output  
  63.         switch (v.getId())   
  64.         {  
  65.             case R.id.Add:  
  66.                 oper = "+";  
  67.                 result = num1 + num2;  
  68.                 break;  
  69.             case R.id.Sub:  
  70.                 oper = "-";  
  71.                 result = num1 - num2;  
  72.                 break;  
  73.             case R.id.Mul:  
  74.                 oper = "*";  
  75.                 result = num1 * num2;  
  76.                 break;  
  77.             case R.id.Div:  
  78.                 oper = "/";  
  79.                 result = num1 / num2;  
  80.                 break;  
  81.             default:  
  82.                 break;  
  83.         }  
  84.         // form the output line  
  85.         Result.setText(num1 + " " + oper + " " + num2 + " = " + result);  
  86.     }  
  87. }  
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


Similar Articles