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.
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("");
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With