Android EditText text changes Listener

If you want “listen” to the text changes from an EditText you have to use the TextWatcher. Bellow I will show you an example of TextWatcher:

//first, we create an EditText
EditText answer = new EditText(this);

//second, we create the TextWatcher
TextWatcher textWatcher = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
       //here, after we introduced something in the EditText we get the string from it
       String answerString = answer.getText().toString();

       //and now we make a Toast
       //modify "yourActivity.this" with your activity name .this
       Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();

    }
};

//third, we must add the textWatcher to our EditText
answer.addTextChangedListener(textWatcher);

 

Menu