I have a program where i have an editText . I want to implement TextWatcher to reflect the changes in the total edittext. I want to show the alert dialog when editbox reach the max character limit 10. When i implement Text watcher it works but it show two alert box when it reach 10 character. here is my code
private TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// This sets a textview to the current length
// mTextView.setText(String.valueOf(s.length()));
}
public void afterTextChanged(Editable s) {
if (s.length() == 10) {
messageBody.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder alert = new AlertDialog.Builder(
SendSms.this);
alert.setMessage("You Cross the limit of 10 Words !");
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.show();
}
}
};
Try this..
have you tried in
onTextChanged
Hi I did such work for phone number, when it reaches 5 digits, it automatically put space. You can utilize my code. Please check below code.
Let me know if you need more help
I have editted the code . Globally declare alert Dialog and check whether alertDialog is already shown and then display the alert
This is common behavior of this method. You need to handle using flag.
Use this code
Other references
TextWatcher events are being fired multiple times
Events of TextWatcher are being called twice
Better is to use setError. Read how to use it.
Simply make the alert instance global it will work fine.
in your onCreate
in your afterTextChanged
Try Following Code :
Global Instance :
TextWatcher :