Android Studio:Java:Calculator - How to clear EditText after I click on a button?

63 Views Asked by At

im really new to this. Im making a Calculator and what I want to achieve is delete the results upon pressing a new button.

I mean:

1+2 = 3. Now the 3 is in the EditText field. If I press "8" then the EditText shows "38"

I don't know how to start..again im really really new to this.

This is what I have now, I know its not that what I need.

    private TextView previousCalculation;
    private EditText display;
    private boolean isResultAvailable = false;



    @Override
    protected void onCreate(Bundle savedInstanceState) {


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

        previousCalculation = findViewById(R.id.previousCalculationView);
        display = findViewById(R.id.displayEditText);
        display.setShowSoftInputOnFocus(false);

    }

I calculate the results by adding the MXparser as a library and I clarify it with the equal button.

    public void equalBtnPush(View view){
    String userExp = display.getText().toString();

    previousCalculation.setText(userExp);

    Expression exp = new Expression(userExp);
    String result = String.valueOf(exp.calculate());

    display.setText(result);
    isResultAvailable = true;
    display.setSelection(result.length());

}

public void OneBtnPush(View view){
    if (isResultAvailable) {
       display.setText("1");
    }
    else{
        updateText(getResources().getString(R.string.oneText));
    }

}

Now the problem is if I click the 1, the result is deleted, and gives me a 1 in the edit text field, but I cannot give anymore 1, like 111, and the value gets always deleted.

0

There are 0 best solutions below