Set InputFilter for white space and maximum length

1k Views Asked by At

I want to use Input filter where I can prevent first space entering AND max length of my edit text limited to 200 characters.

so far I have this: Hot to I put the maxlength into the same filter

 private void setInputFilterForEmailAndPwd(final EditText amountEditText) {
 InputFilter filter = new InputFilter() {
     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  for (int i = start; i < end; i++) {
      if (Character.isSpace(source.charAt(i))) {
   return "";
      }
  }
  
  return null;
     }
 };

 amountEditText.setFilters(new InputFilter[] { filter });
    }



setInputFilterForEmailAndPwd(emailEdit);

1

There are 1 best solutions below

0
Katherina On

I found the solution I put new Input Filter, instead of setInputFilterForEmailAndPwd:

InputFilter filter = new InputFilter() {
     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  for (int i = start; i < end; i++) {
      if (Character.isWhitespace(source.charAt(i))) {
   return "";
      }
  }
  return null;
     }
 };

and then:

 emailEdit.setFilters(new InputFilter[] { filter, new InputFilter.LengthFilter(200) });