0

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);
Katherina
  • 379
  • 1
  • 2
  • 15
  • 1
    try this. – D.J Sep 27 '16 at 11:07
  • I tried that, It doesnt work for me – Katherina Sep 27 '16 at 11:09
  • 1
    try the following snippet :- InputFilter[] filters = new InputFilter[]{new InputFilter.LengthFilter(200), myFirstSpaceFilter}; // myFirstSpaceFilter is the filter that you have already creatd amountEditText.setFilters(filters); – Sachin Rao Sep 27 '16 at 11:14
  • it wont work like that either, what to I need to change in my method setInputFilterForEmailAndPwd in order to work like this? – Katherina Sep 27 '16 at 11:34

1 Answers1

1

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) });
Katherina
  • 379
  • 1
  • 2
  • 15