Simple Calculator (Android)

How to start:

1. Install Android Studio and create empty Activity

2. We will be using class EvaluateEngine from Rhino 

3. Click on build.gradle(Module: app) and set the following parameters:

implementation ‘io.apisense:rhino-android:1.0’
testImplementation ‘junit:junit:4.12’

4.Change the MainActivity Class to the following class:


package learn.hq.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tx;
    Button[] bt = new Button[16];
    TextView tx2;
    Calculator ct = new Calculator();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt[0] = (Button)findViewById(R.id.btnZero);
        bt[1] = (Button)findViewById(R.id.btnOne);
        bt[2] = (Button)findViewById(R.id.btnTwo);
        bt[3] = (Button)findViewById(R.id.btnthree);
        bt[4] = (Button)findViewById(R.id.btnfour);
        bt[5] = (Button)findViewById(R.id.btnfive);
        bt[6] = (Button)findViewById(R.id.btnsix);
        bt[7] = (Button)findViewById(R.id.btnseven);
        bt[8] = (Button)findViewById(R.id.btneight);
        bt[9] = (Button)findViewById(R.id.btnnine);
        bt[10] = (Button)findViewById(R.id.btnC);
        bt[11] = (Button)findViewById(R.id.btnequal);
        bt[12] = (Button)findViewById(R.id.btnPlus);
        bt[13] = (Button)findViewById(R.id.btnminus);
        bt[14] = (Button)findViewById(R.id.btnmult);
        bt[15] = (Button)findViewById(R.id.btnDiv);
        tx = (TextView)findViewById(R.id.textView);
        tx2 = (TextView)findViewById(R.id.textView2);
      for(int j = 0;j<10;j++){
          bt[j].setText(j + " ");
      }
bt[10].setText("CE");
bt[11].setText("=");
bt[12].setText("+");
bt[13].setText("-");
bt[14].setText("*");
bt[15].setText("/");

bt[0].setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

            ct.setNumber("" +0);
            changeText(ct.getNumber()+" ");



    }
});
        bt[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ct.setNumber("" +1);
                changeText(ct.getNumber());

            }
        });
        bt[2].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.setNumber("" +2);
                changeText(ct.getNumber());


            }
        });
        bt[3].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ct.setNumber("" +3);
                changeText(ct.getNumber());

            }
        });

    bt[4].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ct.setNumber("" +4);
            changeText(ct.getNumber());

        }
    });
        bt[5].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ct.setNumber("" +5);
                changeText(ct.getNumber());

            }
        });
        bt[6].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ct.setNumber("" +6);
                changeText(ct.getNumber());

            }
        });
        bt[7].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ct.setNumber("" +7);
                changeText(ct.getNumber());

            }
        });
        bt[8].setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                ct.setNumber("" +8);
                changeText(ct.getNumber());

            }
        });
        bt[9].setOnClickListener(new View.OnClickListener() {
            @Override
            // button clearnumber
            public void onClick(View v) {

                ct.setNumber("" +9);
                changeText(ct.getNumber());

            }
        });
        bt[10].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.clearNumber();
                changeText(ct.getNumber());

            }
        });
        bt[11].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.equals();
                changeText2(ct.getResult()+" ");


            }
        });
        bt[12].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ct.setNumber("+");
                changeText(ct.getNumber()+" ");


            }
        });
        bt[13].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.setNumber("-");
                changeText(ct.getNumber()+" ");


            }
        });
        bt[14].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.setNumber("*");
                changeText(ct.getNumber()+" ");


            }
        });
        bt[15].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ct.setNumber("/");
                changeText(ct.getNumber()+" ");


            }
        });
}
    public void changeText(String id){

        tx.setText(id+" ");

    }
    public void changeText2(String id){

        tx2.setText(id+" ");

    }
}

5. Create Calculator.java class:

package learn.hq.myapplication;

import java.util.List;


public class Calculator {
    private String sumvalue="";
    private double result = 0;
       Calculator(){

    }
   public void setNumber(String value){


           sumvalue += value;

    }

    public String getNumber(){

        return sumvalue;
    }
    public void clearNumber() {
        try{
            if(sumvalue.length() != 0){
                this.sumvalue = sumvalue.substring(0, sumvalue.length() - 1);
            }
        }
       catch(NullPointerException ep){
            ep.printStackTrace();
       }


   }
   public void equals(){
try{
    EvaluateEngine en = new EvaluateEngine();

    result = Math.round(en.evaluate(sumvalue)*100)/100.0;


}
catch(NullPointerException ex){
    ex.printStackTrace();
       }


   }
    public void setResult(double  result){
        this.result = result;
    }
    public double getResult(){
        return result;
    }


}

6.Create Evaluate.java class for evaluating a string from Rhino

package learn.hq.myapplication;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
public class EvaluateEngine {

    private Context rhino;
    private Scriptable scope;

    Double answer;


    /**
     * This function evaluates the string when it is passed as a parameter.
     *
     * @param question The expression is passed to the method
     * @return Returns the evaluated answer in a double variable
     */
    public Double evaluate (String question) {

        Object[] functionParams = new Object[]{question};

        //The js function
        String script = "function evaluate(arithmetic){  return eval(arithmetic)    ;} ";

        Context rhino = Context.enter();

        //disabling the optimizer to better support Android.
        rhino.setOptimizationLevel(-1);

        try {

            Scriptable scope = rhino.initStandardObjects();

            /**
             * evaluateString(Scriptable scope, java.lang.String source, java.lang.String sourceName,
             * int lineno, java.lang.Object securityDomain)
             *
             */
            rhino.evaluateString(scope, script , "JavaScript", 1, null);


            Function function = (Function) scope.get("evaluate", scope);


            answer = (Double) function.call(rhino, scope, scope, functionParams);


        } catch (RhinoException e) {

            e.printStackTrace();

        } finally {
            Context.exit();
        }

        return answer;
    }
}

6. insert following code in res->layout->activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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">

    <Button
        android:id="@+id/btnOne"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnfour"
        app:layout_constraintEnd_toStartOf="@+id/btnTwo"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnZero"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button"
        app:layout_constraintBottom_toTopOf="@+id/btnthree"
        app:layout_constraintEnd_toStartOf="@+id/btnOne"
        app:layout_constraintHorizontal_bias="0.773"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnfive"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnthree"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnsix"
        app:layout_constraintEnd_toStartOf="@+id/btnfour"
        app:layout_constraintHorizontal_bias="0.836"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnsix"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnnine"
        app:layout_constraintEnd_toStartOf="@+id/btnseven"
        app:layout_constraintHorizontal_bias="0.911"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnC"
        android:layout_width="58dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnTwo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.954"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btneight"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="68dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnequal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnseven"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnDiv"
        app:layout_constraintEnd_toStartOf="@+id/btneight"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnequal"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="68dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnmult"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnnine"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnPlus"
        app:layout_constraintEnd_toStartOf="@+id/btnDiv"
        app:layout_constraintHorizontal_bias="0.911"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnPlus"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnminus"
        app:layout_constraintHorizontal_bias="0.911"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnminus"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnmult" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnminus"
        app:layout_constraintEnd_toStartOf="@+id/btnequal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnmult"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="68dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/btnfour"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btnseven"
        app:layout_constraintEnd_toStartOf="@+id/btnfive"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/btnfive"
        android:layout_width="94dp"
        android:layout_height="78dp"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="8dp"
        android:text="@string/button2"
        app:layout_constraintBottom_toTopOf="@+id/btneight"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="276dp"
        android:layout_height="55dp"
        android:layout_marginBottom="8dp"
        android:text="@string/textview"
        app:layout_constraintBottom_toTopOf="@+id/btnOne"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.244"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="116dp"
        android:layout_height="35dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="8dp"
        android:text="@string/textview2"
        app:layout_constraintEnd_toStartOf="@+id/btnC"
        app:layout_constraintHorizontal_bias="0.717"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Now its finished!!!

Smiley face

Leave a Reply

Your email address will not be published. Required fields are marked *