How To Make a Mini Calculator In Android

Introduction

 
In this article, I will create a small application for a mini calculator. Whenever we want to just add, subtract, multiply, or divide two numbers, we usually want to use a mini calculator. So  I am telling you how to create a mini calculator in Android. The following procedure shows how to create a mini calculator on Android.
 
Step 1
 
First of all create a new Android application project using: "File" -> "New"-> "Android Application Project" as shown below.
 
8new.jpg
 
Step 2
 
Now open the "activity_main.xml" file and update it for the mini calculator UI screen as in the following code.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <EditText  
  7.         android:id="@+id/editText1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:ems="10"  
  14.         android:maxLength="10"  
  15.         android:inputType="numberDecimal" />  
  16.     <EditText  
  17.         android:id="@+id/editText2"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentLeft="true"  
  21.         android:layout_alignParentRight="true"  
  22.         android:layout_below="@+id/editText1"  
  23.         android:ems="10"  
  24.         android:maxLength="10"  
  25.         android:inputType="numberDecimal" />  
  26.     <TextView  
  27.         android:id="@+id/textView1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_alignParentBottom="true"  
  31.         android:layout_alignParentLeft="true"  
  32.         android:layout_alignParentRight="true"  
  33.         android:layout_below="@+id/button1"  
  34.         android:text="@string/result"  
  35.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  36.     <Button  
  37.         android:id="@+id/button4"  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_alignBaseline="@+id/button3"  
  41.         android:layout_alignBottom="@+id/button3"  
  42.         android:layout_alignParentRight="true"  
  43.         android:text="@string/divdbtn" />  
  44.     <Button  
  45.         android:id="@+id/button3"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_above="@+id/textView1"  
  49.         android:layout_marginRight="16dp"  
  50.         android:layout_toLeftOf="@+id/button4"  
  51.         android:text="@string/multbtn" />  
  52.     <Button  
  53.         android:id="@+id/button2"  
  54.         android:layout_width="wrap_content"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_above="@+id/textView1"  
  57.         android:layout_marginRight="18dp"  
  58.         android:layout_toLeftOf="@+id/button3"  
  59.         android:text="@string/subbtn" />  
  60.     <Button  
  61.         android:id="@+id/button1"  
  62.         android:layout_width="wrap_content"  
  63.         android:layout_height="wrap_content"  
  64.         android:layout_below="@+id/editText2"  
  65.         android:layout_marginRight="14dp"  
  66.         android:layout_toLeftOf="@+id/button2"  
  67.         android:text="@string/addbtn" />  
  68. </RelativeLayout> 
Step 3
 
After updating the UI screen in "activity_main.xml"  update "strings.xml" witch stores all the strings. Update it as in the following code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">MiniClc</string>  
  4.     <string name="hello_world">Hello world!</string>  
  5.     <string name="menu_settings">Settings</string>  
  6.     <string name="result"></string>  
  7.     <string name="clc">Calculate</string>  
  8.     <string name="addbtn">Add</string>  
  9.     <string name="subbtn">Sub</string>  
  10.     <string name="multbtn">mult</string>  
  11.     <string name="divdbtn">divd</string>  
  12. </resources> 
Step 4
 
Now open the "MainActivity.java" file from "src/com.mcn.minicalculator/MainActivity.java" and update it as in the following code.
  1. package com.example.miniclc;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9. public class MainActivity extends Activity {  
  10.             double a,b,c;  
  11.             Button addbtn,subbtn,mulbtn,divdbtn;  
  12.       @Override  
  13.       protected void onCreate(Bundle savedInstanceState) {  
  14.             super.onCreate(savedInstanceState);  
  15.             setContentView(R.layout.activity_main);  
  16.             final EditText edt1=(EditText) findViewById(R.id.editText1);  
  17.             final EditText edt2=(EditText) findViewById(R.id.editText2);  
  18.             final TextView result= (TextView) findViewById(R.id.textView1);  
  19.              addbtn=(Button) findViewById(R.id.button1);  
  20.              subbtn=(Button) findViewById(R.id.button2);  
  21.              mulbtn=(Button) findViewById(R.id.button3);  
  22.              divdbtn=(Button) findViewById(R.id.button4);  
  23.             addbtn.setOnClickListener(new Button.OnClickListener()  
  24.             {public void onClick  
  25.                   (View  v) { calculate();}  
  26.             private void calculate() {  
  27.                   // TODO Auto-generated method stub  
  28.                     a=Double.parseDouble(edt1.getText().toString());  
  29.                   b=Double.parseDouble(edt2.getText().toString());  
  30.                   c=a+b;  
  31.                   result.setText(Double.toString(c));  
  32.             }});  
  33.             subbtn.setOnClickListener(new Button.OnClickListener()  
  34.             {public void onClick  
  35.                   (View  v) { calculate();}  
  36.             private void calculate() {  
  37.                   // TODO Auto-generated method stub  
  38.                     a=Double.parseDouble(edt1.getText().toString());  
  39.                   b=Double.parseDouble(edt2.getText().toString());  
  40.                   c=a-b;  
  41.                   result.setText(Double.toString(c));  
  42.             }});  
  43.             mulbtn.setOnClickListener(new Button.OnClickListener()  
  44.             {public void onClick  
  45.                   (View  v) { calculate();}  
  46.             private void calculate() {  
  47.                   // TODO Auto-generated method stub  
  48.                     a=Double.parseDouble(edt1.getText().toString());  
  49.                   b=Double.parseDouble(edt2.getText().toString());  
  50.                   c=a*b;  
  51.                   result.setText(Double.toString(c));  
  52.             }});  
  53.             divdbtn.setOnClickListener(new Button.OnClickListener()  
  54.             {public void onClick  
  55.                   (View  v) { calculate();}  
  56.             private void calculate() {  
  57.                   // TODO Auto-generated method stub  
  58.                     a=Double.parseDouble(edt1.getText().toString());  
  59.                   b=Double.parseDouble(edt2.getText().toString());  
  60.                   c=a/b;  
  61.                   result.setText(Double.toString(c));  
  62.             }});  
  63.       }  
  64.       @Override  
  65.       public boolean onCreateOptionsMenu(Menu menu) {  
  66.             // Inflate the menu; this adds items to the action bar if it is present.  
  67.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  68.             return true;  
  69.       }  
Output
 
The output will show on the current screen where you enter the query.
 
If you click on the "Add" button then you will get the addition of both numbers.
 
8Add.jpg
 
And if you click on "Sub" then the result will be the subtraction.
 
8Sub.jpg
 
And if you click on "mult" then you will get the multiplication.
 
8Mul.jpg
 
And after clicking on "divd" you will get the division.
 
8Divd.jpg
 
8Divd2.jpg
 


Similar Articles