Loading, please wait...

A to Z Full Forms and Acronyms

Calculator Application | Android Tutorial

Nov 20, 2020 #Andorid#AndroidStudio, 7054 Views
Android Calculator Application

Calculator Application | Android Tutorial

PREREQUISITE:

  • Installation of Android Studio. 
  • Must be aware of Java language. 
  • Basic concepts of android.

Calculator Application

A calculator is the most essential piece of equipment in our everyday life. Somehow and somewhere we all need a calculator. The areas where the calculator is more prominent are research fields where scientists use scientific calculations, shopkeepers use it for calculating the amount, banks, and many more areas where it is the most important equipment. 

PRINCIPAL ELEMENTS USED IN MAKING OF A CALCULATOR:

  • LinearLayout: It is used as a Layout. It helps in aligning the elements in an organized way. You can give the orientation either horizontally and vertically with the help of android:orientation="vertical/horizontal" a statement. 
  • TextView: It is used to display the text. The text view is used in the application to display the numbers and the calculated result.
  • Button: The button plays an important role in the calculator. The number displayed on the calculator is provided in the form of buttons. Not only numbers but also the arithmetic operator symbol are also provided in the form of buttons. 
  • TableLayout: It arranges the group of view in the rows and columns. It does not display the borders lines of rows and columns. It has table rows.
  • TableRow: It the child of the TableLayout. It always exists in the TableLayout. It can be divided into one or more cells.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#AFE0F6"
        android:gravity="right|bottom"
        android:text="0"
        android:textSize="30sp" />
    <TableLayout
        android:id="@+id/tb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginRight="2pt"
        android:layout_marginLeft="2pt">

        <TableRow
            android:id="@+id/tR1"
            android:layout_width="match_parent"
            android:layout_height="147dp">

            <Button
                android:id="@+id/btn1"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="9"
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:layout_marginRight="2pt"
                android:text="8"
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:layout_marginRight="2pt"
                android:text="7"
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn4"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#DCD06C"
                android:layout_marginRight="1pt"
                android:text="+"
                android:textSize="30sp"
                android:onClick="arithmetic" />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/btn5"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="6"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn6"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="5"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn7"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="4"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn8"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#DCD06C"
                android:text="-"
                android:textSize="30sp"
                android:onClick="arithmetic"/>
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/btn9"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="3"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn10"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="2"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn11"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="1"
                android:textSize="30sp"
                android:onClick="numberclick" />
            <Button
                android:id="@+id/btn12"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#DCD06C"
                android:text="%"
                android:textSize="30sp"
                android:onClick="arithmetic" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn13"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="+/-"
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn14"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="0"
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn15"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#FAF2B0"
                android:text="."
                android:textSize="30sp"
                android:onClick="numberclick" />

            <Button
                android:id="@+id/btn16"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:background="#DCD06C"
                android:text="="
                android:textSize="30sp"
                android:onClick="equalevent" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn17"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:onClick="clear"
                android:background="#FAF2B0"
                android:text="C"
                android:textSize="30sp" />

            <Button
                android:id="@+id/btn18"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:onClick="numberclick"
                android:background="#FAF2B0"
                android:text="()"
                android:textSize="30sp" />

            <Button
                android:id="@+id/btn19"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:onClick="arithmetic"
                android:background="#FAF2B0"
                android:text="/"
                android:textSize="30sp" />

            <Button
                android:id="@+id/btn20"
                android:layout_width="90dp"
                android:layout_height="80dp"
                android:onClick="arithmetic"
                android:background="#DCD06C"
                android:text="X"
                android:textSize="30sp" />
        </TableRow>
    </TableLayout>
</LinearLayout>

Working of Calculator in MainActivity.java file:

  • Initially, create the object of elements such as TextView with the private access specifier. Allegedly, private access specifier is used to taking the rights from the classes to use the element other than the class in which it's declared. 
  • Assign the element's id to both the objects created. 
  • Create two Strings variables to take the values on which the particular arithmetic operation is performed. One variable is declared to store the result of the performed action. One boolean variable is used to see that the text view is empty or not. One more variable is used to store the arithmetic operations.
  • onClick function is created in activity_main.xml file.
  • On the following, onClick functions switch cases are used to perform the calculations.
package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView ed;
    boolean newvalue=true;
    String operatorused="+";
    String Number="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed=findViewById(R.id.edittext);
    }
    public void numberclick(View view)
    {
        if(newvalue)
        {
            ed.setText("");
        }
        newvalue=false;
        String input= ed.getText().toString();
        switch (view.getId())
        {
            case R.id.btn1:
                input=input+"9";
                break;
            case R.id.btn2:
                input=input+"8";
                break;
            case R.id.btn3:
                input=input+"7";
                break;
            case R.id.btn5:
                input=input+"6";
                break;
            case R.id.btn6:
                input=input+"5";
                break;
            case R.id.btn7:
                input=input+"4";
                break;
            case R.id.btn9:
                input=input+"3";
                break;
            case R.id.btn10:
                input=input+"2";
                break;
            case R.id.btn11:
                input=input+"1";
                break;
            case R.id.btn14:
                input=input+"0";
                break;
            case R.id.btn15:
                input=input+".";
                break;
            case R.id.btn13:
                input="-"+input;
                break;
        }
        ed.setText(input);
    }
    public void arithmetic(View view)
    {
        newvalue=true;
        Number=ed.getText().toString();
        switch(view.getId())
        {
            case R.id.btn4: operatorused ="+"; break;
            case R.id.btn8: operatorused="-"; break;
            case R.id.btn19: operatorused="/"; break;
            case R.id.btn20: operatorused="X"; break;
        }
    }
    public void equalevent(View view)
    {
        String anotherNumber=ed.getText().toString();
        double answer=0.0;
        switch (operatorused)
        {
            case "+": answer=Double.parseDouble(Number)+Double.parseDouble(anotherNumber); break;
            case "-": answer=Double.parseDouble(Number)-Double.parseDouble(anotherNumber); break;
            case "/": answer=Double.parseDouble(Number)/Double.parseDouble(anotherNumber); break;
            case "X": answer=Double.parseDouble(Number)*Double.parseDouble(anotherNumber); break;
        }
        ed.setText(answer+"");
    }
    public void clear(View view)
    {
        ed.setText("0");
        newvalue=true;
    }
}

 

                                                                                     

 

 

 

A to Z Full Forms and Acronyms