class TextLengthFilter implements InputFilter {
private int mMaxLength;
Toast mToast;
public TextLengthFilter(int maxLength) {
mMaxLength = maxLength;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
int keep = mMaxLength - (dest.length() - (dend - dstart));
if (keep <= 0) {
if(mToast != null){
mToast.cancel();
mToast = null;
}
mToast = Toast.makeText(getApplicationContext(), R.string.event_edit_activity_prompt, Toast.LENGTH_SHORT);
mToast.show();
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
ToastUtils.showToastShort(R.string.event_edit_activity_prompt, getApplicationContext());
return "";
}
}
ToastUtils.showToastShort(R.string.event_edit_activity_prompt, getApplicationContext());
return source.subSequence(start, keep);
}
}
}
TextLengthFilter filter =new TextLengthFilter(100);
mSearchEditText.setFilters(new InputFilter[]{filter});