Note that there are some explanatory texts on larger screens.

plurals
  1. POeditText.getText().toString() in TextWatcher returns empty string at the first textchange
    text
    copied!<p>I added a <code>textchangelistener</code> to my <code>Edittext</code> in which i check if the text is empty or not. When the text isn't empty and I press on the backspace key, all the text disappears and reappears again. This only occur the first time when backspace is pressed. After that it works like it should.</p> <p>Using Editable s.toString() doesn't solve the issue.</p> <p>What is causing this issue?</p> <p>See the comments in the code for more information: </p> <pre><code>TextWatcher textwatcher = getMandatoryTextWatcher(editText); String text = editText.getText().toString();//At this point String text is not empty. editText.addTextChangedListener(textwatcher); public TextWatcher getMandatoryTextWatcher(final EditText editText) { TextWatcher textwatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //To change body of implemented methods use File | Settings | File Templates. } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //To change body of implemented methods use File | Settings | File Templates. } @Override public void afterTextChanged(Editable s) { try { //The first time this is called(the first softkey backspace press), //text is empty. This causes a weird issue where the character that //was just removed by pressing the backspace, reappears again. //This is only the first time. String text = editText.getText().toString(); if (text.length() &lt;= 0) { editText.setError(errorMandatory); } else if (text.length() &gt; 0) { editText.setError(null); } else { editText.setError(errorMelding); } } catch (NumberFormatException nfe) { editText.setError(errorMeldingTijdensParsen); } } }; return textwatcher; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload