Peri Hartman
Peri Hartman

Reputation: 19484

in beforeTextChange, value already changed

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?

Upvotes: 2

Views: 1202

Answers (2)

Peri Hartman
Peri Hartman

Reputation: 19484

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.

Upvotes: 1

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

try the following code it is working

public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if(count>0)
                Log.e("MIS",""+s.toString().substring(0,count-1));
            }

Upvotes: 1

Related Questions