user2625086
user2625086

Reputation: 221

Clearing edit text data

I have certain edit text fields. I save the data entered in these fields to my database, which opens in another activity.

But I have a problem when I navigate back to the first activity (Using the back button on the hardware of the emulator) to add next record, the edit field data is retained.

I tried onPause() and myEditText.setText("") also. But the dat simple clears off the edit fields but as soon as I click the fields to enter data again the previous data reappears.

I also tried using finish() and everything works except I have to go through all the activities again to enter the data.

Upvotes: 1

Views: 3677

Answers (6)

KDeogharkar
KDeogharkar

Reputation: 10959

Use this:

editText.getText().clear();

Upvotes: 2

Paras Jain
Paras Jain

Reputation: 49

You can use:

myEditText.setText(null);

Upvotes: 0

17Coder
17Coder

Reputation: 79

after onclick of any action do below step

((EditText) findViewById(R.id.yoursXmlId)).setText("");

or else

write this in XML file

EditText

android:hint="Enter Name" />

i did this way.try

Upvotes: 0

Guru
Guru

Reputation: 186

Try this one also.

 submit.setOnClickListener(new View.OnClickListener() {

@Override
            public void onClick(View v) {

                    String uid=editText1.getText().toString();
                    String pwd=editText2.getText().toString();
                  editText1.setText("");
                    editText2.setText("");
                        }
});
}

Upvotes: 3

amorenew
amorenew

Reputation: 10896

you can use this library to clear text by a clear icon enter image description here

http://droidparts.org/widgets.html#clearableedittext

Upvotes: 2

Shobhit Puri
Shobhit Puri

Reputation: 26007

Just to make sure that I understood you clearly. You enter text in EditText boxes. Then press a button which takes you to a new activity. But, when you go back to the old activity the text in the EditText boxes doesn't get clear. Try to do editText.setText(""); when you click the button. I know you said that you tried it, but did you try it inside the function which listens for the button click?

public EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText = (EditText)findViewById(R.id.editText1);
    editText.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // As soon as button is clicked, set is as empty
    editText.setText("");
}

Try and see if it makes a difference.

Upvotes: 2

Related Questions