Hello, i faced some problem in building the android calculator. The problems like this:
1) If i click 2 + 3, it worked well, and screen will show the 5.** BUT! when i clicked + and + and + and +...... it will continuosly add the answer. How can i solve it
2) if i click 2 + 3, it worked well, and screen will show the 5. **BUT!
how if i 2 + - , it will perform 2 + 2 - ....

Others doesnt have any problem, anyone may help

MainActivity.java file

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private TextView disp, dispMini;
    private Boolean inserting = false;
    private operate operation;
    private static final String digit = "0123456789.";
    private static final String optr = "+-X/.";

    DecimalFormat df = new DecimalFormat("@###########");


    @Override
    protected void onCreate(Bundle icicle) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);                   // hide the status bar and other

        super.onCreate(icicle);
        setContentView(R.layout.activity_main);


        operation = new operate();
        disp = (TextView) findViewById(R.id.disp);
        dispMini = (TextView) findViewById(R.id.dispMini);


        df.setMinimumFractionDigits(0);         //automatically set fraction to 0   (0.0)
        df.setMinimumFractionDigits(1);         //automatically set fraction to 1   (0.1)
        df.setMinimumFractionDigits(8);         //automatically set faction to 8    (0.11111111)

        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
        findViewById(R.id.btn5).setOnClickListener(this);
        findViewById(R.id.btn6).setOnClickListener(this);
        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.btn8).setOnClickListener(this);
        findViewById(R.id.btn9).setOnClickListener(this);
        findViewById(R.id.btn0).setOnClickListener(this);

        findViewById(R.id.btnAdd).setOnClickListener(this);
        findViewById(R.id.btnSub).setOnClickListener(this);
        findViewById(R.id.btnMul).setOnClickListener(this);
        findViewById(R.id.btnDiv).setOnClickListener(this);
        findViewById(R.id.btnDot).setOnClickListener(this);
        findViewById(R.id.btnToggle).setOnClickListener(this);
        findViewById(R.id.btnEquals).setOnClickListener(this);
        findViewById(R.id.btnClear).setOnClickListener(this);
        findViewById(R.id.btnMod).setOnClickListener(this);
        findViewById(R.id.btnSquared).setOnClickListener(this);
        findViewById(R.id.btnCube).setOnClickListener(this);
        findViewById(R.id.btnSquareRoot).setOnClickListener(this);
        findViewById(R.id.btnOver).setOnClickListener(this);
        findViewById(R.id.btnDel).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        String buttonPressed = ((Button) v).getText().toString();
        v.startAnimation(animAlpha);

        if (digit.contains(buttonPressed)) {
            if (inserting) {
                if (buttonPressed.equals(".") && disp.getText().toString().contains(".")) {   //to prevent entering multiple dot
                }

                else {
                    disp.append(buttonPressed);
                }
            }

            else {
                if (buttonPressed.equals(".")) {
                    disp.setText(0 + buttonPressed);      //add zero to the front if dot is entered (means .3 will become 0.3)
                }

                else {
                    disp.setText(buttonPressed);
                }

                inserting = true;
            }
        }

        else if(v.getId() == R.id.btnDel)               //if backspace button ("DEL") is clicked, delete one number
        {
            String toDelete = disp.getText().toString();

            if(toDelete.length() > 1) {
                toDelete = toDelete.substring(0, toDelete.length() - 1);
                disp.setText(toDelete);
            }

            else if(toDelete.length() <=1){
                disp.setText("0");
            }
        }

        else {
            //to display the all input value and operator in minimized view
            if(optr.contains(buttonPressed)) {
                if (dispMini.getText() != null) {
                    String str1 = dispMini.getText().toString();
                    String str2 = disp.getText().toString();
                    dispMini.setText(str1.concat(str2.concat(buttonPressed)));
                } else {
                    String str = disp.getText().toString();
                    dispMini.setText(str.concat(buttonPressed));
                }
            }

            //do the operation
            if (inserting) {
                operation.setOperand(Double.parseDouble(disp.getText().toString()));
                inserting = false;
            }

            operation.performOperation(buttonPressed);
            disp.setText(df.format(operation.getResult()));

        }

        if(v.getId() == R.id.btnClear || v.getId()==R.id.btnEquals)     //clean textview if clean button or equal button clicked
        {
            dispMini.setText("");
        }

    }
}

Operate.java file

public class operate {

    private double dispValue;
    private double tempValue;           //used while waiting the value input
    private String tempOperator;        //used while waiting the operator enter

    public static final String add = "+";
    public static final String sub = "-";
    public static final String mul = "X";
    public static final String div = "/";

    public static final String clr = "AC" ;
    public static final String mod = "%" ;
    public static final String squareroot = "√";
    public static final String squared = "x²";
    public static final String cube = "x³";
    public static final String over = "1/x";
    public static final String toogle = "+/-";


    // constructor
    public operate() {
        // initialize variables upon start
        dispValue = 0;
        tempValue = 0;
        tempOperator = "";
    }

    public void setOperand(double operand) {
        dispValue = operand;
    }

    public double getResult() {
        return dispValue;
    }

    public String toString() {
        return Double.toString(dispValue);
    }

    protected double performOperation(String operator) {

        switch (operator){

            case clr:
                dispValue = 0;
                tempOperator = "";
                tempValue = 0;
                break;

            case squareroot:
                dispValue = Math.sqrt(dispValue);
                break;

            case squared:
                dispValue = dispValue * dispValue;
                break;

            case cube:
                dispValue = dispValue * dispValue * dispValue;
                break;

            case over:
                if (dispValue != 0) {
                    dispValue = 1 / dispValue;
                }
                break;

            case toogle:
                dispValue = -dispValue;
                break;

           default:
                simpleOperation();
                tempOperator = operator;
                tempValue = dispValue;
                break;
        }
        return dispValue;
    }

    protected void simpleOperation() {

        switch (tempOperator){
            case add:
                dispValue = tempValue + dispValue;
                break;

            case sub:
                dispValue = tempValue - dispValue;
                break;

            case mul:
                dispValue = tempValue * dispValue;
                break;

            case div:
                if (dispValue != 0) {
                    dispValue = tempValue / dispValue;
                }
                break;

            case mod:
                dispValue = tempValue % dispValue;
                break;
        }

    }
}

I know this is going to sound basic, but add some breakpoints into areas where the operations are being performed. Then debug the code and add watches to the data variables.

By watching each variable, you will be able to narrow down where the problem is, and then post a more concise and directed query.

At the moment, you have posted too much code for people to engage.

Don't have time to read through your code properly but I'd probably do something like default tempValue to null, reset tempValue to null at the end of an operation and then check if tempValue is null before performing an operation. This would ensure that the operators only actually operate when there is a newly entered value upon which to operate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.