Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset code for EditText and TextView for android

Tags:

android

I'm new in programming. I did a simple android app which ask user to put 2 numbers, do calculations, display the answer by clicking calculate button. Now I'm trying to set up reset button to clear all the fields. Checked for the solution on stack overflow, but still cannot figure out how to do it. This is my code:

 public void calculate(View view)
 {

    EditText number1 = (EditText)findViewById(R.id.num1ID);
    EditText number2 = (EditText)findViewById(R.id.num2ID);

    Double num1Value = Double.parseDouble(number1.getText().toString());
    Double num2Value = Double.parseDouble(number2.getText().toString());

    Double resultValue = num1Value - num2Value;
    TextView resultDisplay = (TextView)findViewById(R.id.resultID);

    resultDisplay.setText(Double.toString(resultValue));
    }

Thank you.

like image 420
nick Avatar asked Nov 13 '15 07:11

nick


2 Answers

Just add button resetButton to layout

Button resetButton = (Button) findViewById(R.id.btnReset); 
    resetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            number1.setText("");
            number2.setText("");
            resultDisplay.setText("");
        }
    });
like image 161
Tran Vinh Quang Avatar answered Sep 30 '22 17:09

Tran Vinh Quang


Have a method like this

public void resetTextViews() {
      number1.setText("");
      number2.setText("");
      resultDisplay.setText("");
}

Then just set an on click listener on your button that calls that method.

like image 41
Andy Joyce Avatar answered Sep 30 '22 16:09

Andy Joyce