Poly Bug
Poly Bug

Reputation: 1542

Clear TextView from within TextWatcher method

I have a "current password" EditText and an error TextView which displays an error message. I want to clear the error message when the EditText contents change, usually when the user types in another letter or erases something.

The error TextView is not being cleared even though the system prints the messages. The TextView is only being cleared when the EditText loses focus.

Why is this happening? Are these methods being executed on a different thread? How should I clear the TextView immediately after the user types in a character?

    mCurrentPasswordEditText = (EditText) view.findViewById(R.id.current_password_edit_text);
    mCurrentPasswordErrorTextView = (TextView) view.findViewById(R.id.current_password_error_text_view);
    mCurrentPasswordEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //mCurrentPasswordErrorTextView.setText("");
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mCurrentPasswordErrorTextView.setText("");
        }
        @Override
        public void afterTextChanged(Editable s) {
            System.out.println("called multiple times.");
            mCurrentPasswordErrorTextView.setText("");
        }
    });

Upvotes: 0

Views: 467

Answers (1)

Poly Bug
Poly Bug

Reputation: 1542

The solution is very weird. I think this is a bug!

Instead of replacing the TextView with an empty string "", I put an extra space " ", which cleared the text immediately! *smh

mCurrentPasswordEditText = (EditText) view.findViewById(R.id.current_password_edit_text);
mCurrentPasswordErrorTextView = (TextView) view.findViewById(R.id.current_password_error_text_view);
mCurrentPasswordEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //mCurrentPasswordErrorTextView.setText("");
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //mCurrentPasswordErrorTextView.setText("");
    }
    @Override
    public void afterTextChanged(Editable s) {
        mCurrentPasswordErrorTextView.setText(" ");
    }
});

Upvotes: 1

Related Questions