2

I need a reset button that reset all the editText boxes on click to the initial state. at the moment i have this:

    main_xml
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ClearButton"
    android:id="@+id/ClearButton"
    android:onClick="clear"/>

java

     public void clear(View v) {
        TextBox1.setText("0.0");
        TextBox2.setText("0.0");
    } 

i am not very happy with this reset because i have on startup 0.0 in the editText boxes anyway. i would like to implement the reset somewhere here:

   input3 = String.valueOf(TextBox1.getText());
    flowRate.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input3.equals("0.0") || input3.equals(""))) {
                TextBox1.setText("");
            }
            input3 = (String.valueOf(TextBox1.getText()));
            if ((!hasFocus) && (input3.equals(""))){
                TextBox1.setText("0.0");
            }
        }
    });
    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input2 = String.valueOf(TextBox2.getText());
    TextBox2.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input2.equals("0.0") || input2.equals(""))) {
                TextBox2.setText("");
            }
            input2 = (String.valueOf(TextBox2.getText()));
            if ((!hasFocus) && (input2.equals(""))){
                TextBox2.setText("0.0");
            }
        }
    });

at the moment when i reset and my focus is on a editText Box i have to delete the 0.0 first before i can write a value in the box. i would like on reset that the focus come out the editText box and everything is on the initial state.

Pascal Westrich
  • 35
  • 1
  • 1
  • 6
  • 2
    create any method for eg. `reset()`, now set values you want to your edittexts in that method, now call it from any where but make sure that call is from mainthread. – karan Jul 29 '15 at 11:48

2 Answers2

0

To the focus come out from editbox, you just have to use clearFocus().

TextBox1.clearFocus();
TextBox2.clearFocus();

Here is my code

public class MainActivity extends AppCompatActivity {
String input1, input2, input3;
EditText tankVolume, fillingPressure, flowRate;
TextView fillingTime;
Button calculate, ClearButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tankVolume=(EditText)findViewById(R.id.tankVolume);
    fillingPressure=(EditText)findViewById(R.id.fillingPressure);
    flowRate=(EditText)findViewById(R.id.flowRate);
    fillingTime=(TextView)findViewById(R.id.fillingTime);
    calculate=(Button)findViewById(R.id.calculate);

    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input3 = String.valueOf(flowRate.getText());
    flowRate.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input3.equals("0.0") || input3.equals(""))) {
                flowRate.setText("");
            }
            input3 = (String.valueOf(flowRate.getText()));
            if ((!hasFocus) && (input3.equals(""))){
                flowRate.setText("0.0");
            }
        }
    });
    // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input2 = String.valueOf(fillingPressure.getText());
    fillingPressure.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input2.equals("0.0") || input2.equals(""))) {
                fillingPressure.setText("");
            }
            input2 = (String.valueOf(fillingPressure.getText()));
            if ((!hasFocus) && (input2.equals(""))){
                fillingPressure.setText("0.0");
            }
        }
    });
     // remove on touch the value entries in the Flow rate field to start calculation
    // if no value is entered the field will return to his normal state (0.0)
    input1 = String.valueOf(tankVolume.getText());
    tankVolume.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if ((hasFocus)&&(input1.equals("0.0") || input1.equals(""))) {
                tankVolume.setText("");
            }
            input1 = (String.valueOf(tankVolume.getText()));
            if ((!hasFocus) && (input1.equals(""))){
                tankVolume.setText("0.0");
            }

        }
    });

    // calculate the filling time
    calculate.setOnClickListener(new Button.OnClickListener()

    {
        public void onClick
                (View v) {
            calculate();
        }
    });
}

private void calculate()
{

    Double value1 = Double.parseDouble(tankVolume.getText().toString());
    Double value2 = Double.parseDouble(fillingPressure.getText().toString());
    Double value3 = Double.parseDouble(flowRate.getText().toString());
    Double calculatedValue = (value1*value2)/value3;


    fillingTime.setText(calculatedValue.toString());
}



    // reset everything to zero
    public void clear(View v) {
        tankVolume.setText("0.0");
        fillingPressure.setText("0.0");
        flowRate.setText("0.0");
        fillingTime.setText("0");
        tankVolume.clearFocus();
        fillingPressure.clearFocus();
        flowRate.clearFocus();
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

EDIT

In your case you should use hints in xml such as

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="0.0"/>

Then you can only use setText("") for each TextView into your clear() method, and remove the all setOnFocusChangeListener, because it will no longer be used.

Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • the problem now is on clearFocus my keyboard is switching from numeric to qwerty keyboard and on focus it switches back. – Pascal Westrich Jul 29 '15 at 13:05
  • Where exactly are you trying to clear the focus of both `EditText`? – Lennon Spirlandelli Jul 29 '15 at 15:16
  • i edit your text and applied my code. //set everything to zero - is the location – Pascal Westrich Jul 29 '15 at 19:51
  • is it possible to reset the whole activity to its original state on clickButton? thats maybe less difficult. – Pascal Westrich Jul 29 '15 at 20:01
  • Taking a look your code, I suppose you need to use hints, like the answer below. I'm editing my answer in order to show you the best way to do so. – Lennon Spirlandelli Jul 29 '15 at 20:25
  • Thank you very much for your answer. I use hints in my EditText. Under the 0.0 in the EditText for fillingPressure, i putt the hint "bar", in EditText for flowRate i putt the hint" l/min" and so on. the 0.0 is just a overlay and if i click in the editText box the 0.0 disappears and the hint is viewable until number input. after the calculation i would like to reset without focus to any EditText box and show the 0.0 again like on the initial start of the calculation. everything is working, the problem is when i clear Focus the keyboard change to qwerty and not stay on numeric. – Pascal Westrich Jul 29 '15 at 21:44
  • Using hints, you no longer need to clear focus. You just have to set `""` into all editTexts to clear the fields. Remove all `setOnFocusChangeListener ` and `clearFocus` you are using. – Lennon Spirlandelli Jul 30 '15 at 14:03
-1

Change from

public void clear(View v) {
        TextBox1.setText("0.0");
        TextBox2.setText("0.0");
    } 

to

public void clear(View v) {
        TextBox1.setHint("0.0");
        TextBox2.setHint("0.0");
    } 

It might help you.

André Schild
  • 4,592
  • 5
  • 28
  • 42
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46