How to build a simple Calculator app using Android Studio

Today we are going to create a Simple Calculator App using Android Studio. The calculator app is a very good project for a new developer who wants to learn Android app development.

We are going to use Java Programming Language to develop a Simple Calculator App to perform simple arithmetic operations like multiplication, addition, subtraction, division, etc.

We are going to provide a complete Source Code for Calculator App using Android Studio.

Step 01:

Create a new project in Android Studio to create a simple calculator app.

Note: The code provided in this article is in Java language, so select java language while creating a new project in android studio.

Step 02:

Now edit the activity_main.xml file and copy and paste the code given below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"
<RelativeLayout 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:layout_marginLeft="25dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="25dp"
    tools:context=".RelativeLayout">
    <TextView
        android:id="@+id/ReserveText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50dp"
        android:layout_marginTop="30dp"
        android:text=""
        />
    <EditText
        android:id="@+id/Answer"
        android:layout_below="@id/ReserveText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:textSize="50dp"></EditText>
    <Button
        android:id="@+id/Button1"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Answer"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#C7EFEA"
        android:text="1"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button2"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Answer"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toRightOf="@id/Button1"
        android:backgroundTint="#C7EFEA"
        android:text="2"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button3"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Answer"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toRightOf="@id/Button2"
        android:backgroundTint="#C7EFEA"
        android:text="3"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonPlus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Answer"
        android:layout_alignParentRight="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="20px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#6FBAAAAA"
        android:text="+"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button4"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button1"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#C7EFEA"
        android:text="4"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button5"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button2"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toEndOf="@+id/Button4"
        android:backgroundTint="#C7EFEA"
        android:text="5"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button6"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button3"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toEndOf="@id/Button5"
        android:backgroundTint="#C7EFEA"
        android:text="6"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonMinus"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/ButtonPlus"
        android:layout_alignParentRight="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="20px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#6FBAAAAA
        android:text="-"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button7"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button4"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#C7EFEA"
        android:text="7"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button8"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button5"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toEndOf="@+id/Button7"
        android:backgroundTint="#C7EFEA"
        android:text="8"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button9"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button6"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toEndOf="@id/Button8"
        android:backgroundTint="#C7EFEA"
        android:text="9"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonMultiply"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/ButtonMinus"
        android:layout_alignParentRight="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="20px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#6FBAAAAA"
        android:text="*"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonDot"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button7"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#C7EFEA"
        android:text="."
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/Button0"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button8"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="10px"
        android:layout_marginBottom="0px"
        android:layout_toEndOf="@id/ButtonDot"
        android:backgroundTint="#C7EFEA"
        android:text="0"
        android:textColor="@color/black"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonDivide"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ButtonMultiply"
        android:layout_alignParentRight="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="20px"
        android:layout_marginBottom="0px"
        android:backgroundTint="#6FBAAAAA"
        android:text="/"
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonEqual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button0"
        android:layout_marginStart="20px
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="20px"
        android:layout_toStartOf="@id/ButtonClear"
        android:backgroundTint="#44D310"
        android:text="="
        android:textSize="20dp"></Button>
    <Button
        android:id="@+id/ButtonClear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ButtonDivide"
        android:layout_alignParentRight="true"
        android:layout_marginStart="20px"
        android:layout_marginTop="20px"
        android:layout_marginEnd="20px"
        android:layout_marginBottom="20px"
        android:backgroundTint="#6F369AF1"
        android:text="CLEAR"
        android:textSize="20dp"></Button>
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ButtonClear"
        android:layout_centerHorizontal="true"
        android:fontFamily="casual"
        android:text="EHTISHAM ALI"
        android:textColor="#000000"
        android:textSize="30dp" />
</RelativeLayout>

The front end of our app is ready. Now we have to work on the backend to make the Simple Calculator App work.

Must Read: How to create Custom Exit Dialog in Android Studio?

Now we are going to edit our file. This file contains Java code. We have different functions in this code which will perform specific tasks.

Step 03 to create calculator app using android studio:

Now edit your MainActivity.java file and copy and paste the code given below.

MainActivity.java

package com.freetoreads.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class RelativeLayout extends AppCompatActivity {

    Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonequal, buttonclear, buttonmultiply, buttondivide, buttonadd, buttonsubtract, buttondot;
    EditText numanswer;
    TextView reservetext;
    float num1, num2;
    boolean isadd, issubtract, ismultiply, isdivide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative_layout);

        button0 = (Button) findViewById(R.id.Button0);
        button1 = (Button) findViewById(R.id.Button1);
        button2 = (Button) findViewById(R.id.Button2);
        button3 = (Button) findViewById(R.id.Button3);
        button4 = (Button) findViewById(R.id.Button4);
        button5 = (Button) findViewById(R.id.Button5);
        button6 = (Button) findViewById(R.id.Button6);
        button7 = (Button) findViewById(R.id.Button7);
        button8 = (Button) findViewById(R.id.Button8);
        button9 = (Button) findViewById(R.id.Button9);
        buttondot = (Button) findViewById(R.id.ButtonDot);
        buttonclear = (Button) findViewById(R.id.ButtonClear);
        buttonequal = (Button) findViewById(R.id.ButtonEqual);
        buttondivide = (Button) findViewById(R.id.ButtonDivide);
        buttonmultiply = (Button) findViewById(R.id.ButtonMultiply);
        buttonadd = (Button) findViewById(R.id.ButtonPlus);
        buttonsubtract = (Button) findViewById(R.id.ButtonMinus);
        numanswer = (EditText) findViewById(R.id.Answer);
        reservetext = (TextView) findViewById(R.id.ReserveText);

        buttondot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + ".");
            }
        });

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "0");
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "1");
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "2");
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "3");
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "4");
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "5");
            }
        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "6");
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "7");
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "8");
            }
        });

        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText(numanswer.getText() + "9");
            }
        });

        buttonclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numanswer.setText("");
                reservetext.setText("");
                isadd = false;
                issubtract = false;
                ismultiply = false;
                isdivide = false;
            }
        });

        buttonadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(numanswer == null) {
                    numanswer.setText("");
                } else {
                    num1 = Float.parseFloat(numanswer.getText() + "");
                    isadd = true;
                    reservetext.setText(num1 + " + ");
                    numanswer.setText(null);
                }
            }
        });

        buttonsubtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1 = Float.parseFloat(numanswer.getText() + "");
                issubtract = true;
                reservetext.setText(num1 + " - ");
                numanswer.setText(null);
            }
        });

        buttonmultiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1 = Float.parseFloat(numanswer.getText() + "");
                ismultiply = true;
                reservetext.setText(num1 + " * ");
                numanswer.setText(null);
            }
        });

        buttondivide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1 = Float.parseFloat(numanswer.getText() + "");
                isdivide = true;
                reservetext.setText(num1 + " / ");
                numanswer.setText(null);
            }
        });

        buttonequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num2 = Float.parseFloat(numanswer.getText() + "");

                if (isadd == true) {
                    reservetext.setText(num1 + " + " + num2);
                    numanswer.setText("= " + (num1 + num2));
                }

               else if (issubtract == true) {
                    reservetext.setText(num1 + " - " + num2);
                    numanswer.setText("= " + (num1 - num2));
                }

                else if (ismultiply == true) {
                    reservetext.setText(num1 + " * " + num2);
                    numanswer.setText("= " + (num1 * num2));
                }

                else if (isdivide == true) {
                    reservetext.setText(num1 + " / " + num2);
                    numanswer.setText("= " + (num1 / num2));
                }
            }
        });
    }
}

Conclusion: Creating Calculator app using Android Studio

Thank you for reading this article. So readers we have successfully created a Calculator App using Java.

If you have any queries about this Simple Calculator App using Android Studio.

If you are not able to understand the Source Code for Calculator App then please leave a comment we will reply to you ASAP.

Leave a Reply