Android: Action listener on EditText change

8 09 2012

If business logic requires to trigger any kind of action right after the user changes text in EditText View, the TextWatcher listener can be used. Good example of use is filter, that updates when user provides filtering criteria.

Add textChanged listener to EditText:

myEditText.addTextChangedListener(onSearchFieldTextChanged);

Implement listener to perform desired actions:

TextWatcher onSearchFieldTextChanged = new TextWatcher(){
	public void afterTextChanged(Editable s) {
		//your business logic after text is changed
	}
	public void beforeTextChanged(CharSequence s, int start, int count, int after){
		//your business logic before text is changed
	}

	public void onTextChanged(CharSequence s, int start, int before, int count){
		//your business logic while text has changed
	}
};

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

Advertisements







%d bloggers like this: