Reputation: 57
I have one edittext, i want to set limit to it of 160 char. and if 161th char has typed by user then alert need to show. upto this everything working fine also i am deleting 161th char successfully . but it deletes only last char , if i enter 161th char inside paragraph i want to delete that one not last one.
What i tried :
Edt_txt_keyIn.getText().delete(length - 1, length);
Upvotes: 0
Views: 505
Reputation: 1
Or maybe you can try android:maxLength="160"
if that suits your needs
Upvotes: 0
Reputation: 1135
It's better to use TextWatcher :
Edt_txt_keyIn.addTextChangedListener(new TextWatcher() {
private int position;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
position = start;
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 160) {
Edt_txt_keyIn.setText(s.delete(position, position + 1));
}
}
});
Upvotes: 1