Programming Examples

Code form Android Calculator Design and Program


Android application to implements simple calculator

Solution

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/tableLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:textSize="40dp"
        app:layout_constraintTop_toTopOf="parent" />
    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent">

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

            <Button
                android:id="@+id/per"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="%" />

            <Button
                android:id="@+id/btn_ce"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="CE" />

            <Button
                android:id="@+id/btn_c"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:id="@+id/btn_div"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_weight="1"
                android:text="/" />
        </TableRow>

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

            <Button
                android:id="@+id/btn_7"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/btn_8"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/btn_9"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:id="@+id/btn_mul"
                android:onClick="operatorEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_weight="1"
                android:text="x" />
        </TableRow>

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

            <Button
                android:id="@+id/btn_4"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="4" />

            <Button
                android:id="@+id/btn_5"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/btn_6"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/btn_sub"
                android:onClick="operatorEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_weight="1"
                android:text="-" />
        </TableRow>

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

            <Button
                android:id="@+id/btn_1"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/btn_2"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/btn_3"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/btn_sum"
                android:onClick="operatorEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="+" />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/btn_00"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="00" />

            <Button
                android:id="@+id/btn_0"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="0" />

            <Button
                android:id="@+id/btn_dot"
                android:onClick="numberEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="." />

            <Button
                android:id="@+id/btn_equal"
                android:onClick="equalEvent"
                android:layout_width="30pt"
                android:layout_height="30pt"
                android:layout_marginRight="3dp"
                android:layout_weight="1"
                android:text="=" />
        </TableRow>
    </TableLayout>


</androidx.constraintlayout.widget.ConstraintLayout>



MainActivity.java

package com.example.demo2;

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 MainActivity extends AppCompatActivity {
    TextView editText;
    boolean isNewOp=true;
    String opr="",oldvalue="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.output);

    }

    public void numberEvent(View view) {
        if(isNewOp)
            editText.setText("");
        isNewOp=false;
        String number=editText.getText().toString();
        switch(view.getId())
        {
            case R.id.btn_0:
                number+="0";
                break;
            case R.id.btn_00:
                number+="00";
                break;
            case R.id.btn_1:
                number+="1";
                break;
            case R.id.btn_2:
                number+="2";
                break;
            case R.id.btn_3:
                number+="3";
                break;
            case R.id.btn_4:
                number+="4";
                break;
            case R.id.btn_5:
                number+="5";
                break;
            case R.id.btn_6:
                number+="6";
                break;
            case R.id.btn_7:
                number+="7";
                break;
            case R.id.btn_8:
                number+="8";
                break;
            case R.id.btn_9:
                number+="9";
                break;
            case R.id.btn_dot:
                number+=".";
                break;
        }
        editText.setText(number);
    }

    public void operatorEvent(View view) {
        isNewOp=true;
        oldvalue=editText.getText().toString();
        switch(view.getId()) {
            case R.id.btn_sum:
                opr="+";
                break;
            case R.id.btn_sub:
                opr="-";
                break;
            case R.id.btn_mul:
                opr="*";
                break;
            case R.id.btn_div:
                opr="/";
                break;
        }

    }

    public void equalEvent(View view) {

        Double num1=Double.parseDouble(editText.getText().toString());
        Double num2=Double.parseDouble(oldvalue);
        Double ans=0.0;
        switch(opr)
        {
            case "+":
                ans=num1+num2;
                break;
            case "-":
                ans=num1-num2;
                break;
            case "*":
                ans=num1*num2;
                break;
            case "/":
                ans=num1/num2;
                break;
        }
        editText.setText(String.valueOf(ans));


        /*
        Calculable calc = new ExpressionBuilder("(45+9)/8").build()
        double result1=calc.calculate();

         */
       
    }
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad