0

I'm trying to get what modification user has made to EditText, either insert or delete. I use TextWatcher but I don't get right result, moreover sometimes "getChar(start, end) has end before start" error.

editText = (EditText) findViewById(R.id.MyEditText);
editText.addTextChangedListener(new TextWatcher() {

    @override
    public void afterTextChanged(Editable s){}

    @override
    public void beforeTextChanged(CharSequence s, int start, int count, int after){

        showToast("text removed: " + s.subSequence(start, count));
    }

    @override
    public void onTextChanged(CharSequence s, int start, int before, int count){

        showToast("text added: " + s.subSequence(start, count));

    }



}

As you can see I use beforeTextChanged to get any text that's removed by user, and onTextChanged for insertion. Please shed some light here. Thanks!

API is right here: http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)

EDIT:

I seem to figure it out...it's quite silly: s.subSequence(start, count)) should really be s.subSequence(start, start+count))

Arch1tect
  • 4,128
  • 10
  • 48
  • 69

2 Answers2

0

Just keep your functions inside the afterTextChanged and see what happens

Sample Code

seachbox.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

            fillData(SEARCH_ORDER ,s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub


    });

I hope it will work

Mohit Rakhra
  • 165
  • 9
  • ?? what's `fillData(SEARCH_ORDER ,s.toString());`? and there's only `Editable s` in `afterTextChanged` so I don't see how this callback help.. ` – Arch1tect Jan 27 '14 at 04:17
  • That's the method which I had implemented. You just put up your code over there.. – Mohit Rakhra Jan 27 '14 at 05:39
0

Try this, I'm not sure whether you want the remaining word(after insert/update) or the letter(added/removed).

public class MainActivity extends Activity implements TextWatcher {

private EditText myEditText;
private String inputText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myEditText = (EditText) findViewById(R.id.testEditText);
    myEditText.addTextChangedListener(this);
}

@Override
public void afterTextChanged(Editable s) {      
    if (inputText.length() < s.toString().length()) {
        Toast.makeText(
                this,
                ("Text Added: " + s.toString().substring(inputText.length(),
                        s.toString().length())), Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(
                this,
                ("Text Removed: " + inputText.substring(s.toString().length(),
                        inputText.length())), Toast.LENGTH_SHORT).show();
    }
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    inputText = s.toString();
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

}

Rakhita
  • 4,493
  • 1
  • 16
  • 15
  • use `s.toString().substring(inputText.length() - (s.toString().length()-1 - start), s.toString().length() - (s.toString().length()-1 - start))` to allow character insertion in the middle of the EditText – natinusala Apr 11 '17 at 13:31