I have a TextWatcher on an EditText. In beforeTextChanged, I’m finding that the EditText value has already been changed. Here’s a snip of code:
@Override
public void beforeTextChanged (CharSequence s, int start, int lengthBefore, int lengthAfter)
{
// restoreValue = text.getText().toString();
String restoreValue = s.toString();
System.out.println ("restore |" + restoreValue + "|");
}
In this function the debug output shows a modified string, not the original value of the EditText. It is the same whether I obtain the value from “text” (which is the EditText widget) or from “s”.
Anyone have a possible reason for this?
Turns out the answer is simple. Android is trying to be clever, figuring that your insertion is supposed to be a word, and adding a space to separate it from the other text. Thus, first it inserts the space (possibly two spaces – one at each end of the selected area), and then it inserts the paste-buffer text.
Each of these actions causes a separate call to beforeTextChanged. Same for onTextChanged and afterTextChanged.
So, for a paste action, you can get up to three sets of callbacks.