Search code examples
androidandroid-edittextmaxlengthrequired-fieldnonblank

Android: need to validate an edittext for non-blank input


If I want to enforce a maximum length of input in an EditText field, I can use the maxLength attribute. But if I want to enforce a minimum length (in my case, simply non-blank), I find no corresponding minLength attribute.

I've also looked at the various 'inputType' attributes (phone, password, etc) but I don't see anything like 'required' or 'non-blank'.

I can't believe this would require a custom input filter; it's more likely the answer is so obvious it just doesn't get written down in the form I'm asking the question.


Solution

  • You can simply check that yourField.text() is not equivalent to null or "" at the point of submission. If it is, prompt the user to input something.