Validation using default feature of EditText

Default Validation EditText

I found a good article here on how to use the default feature of EditText to show the error. While writing the validation on EditText on a button click event or TextChanged event instead of showing alert dialog if you want to show error we can use the default feature of EditText as

EditText username = (EditText)findViewById(R.id.uname);
if( username.getText().toString().length() == 0 )
    username.setError( "Empty Text Field");

sample image

Validation

Hope it helps somebody 🙂

You may be also interested in

Validate a URL / Website name in EditText in Android?

Validate a URL / Website

To validate URL/ Website name entered in a EditText, this following works fine…

private boolean isValidUrl(String url) {
      Pattern p = Patterns.WEB_URL;
      Matcher m = p.matcher(url.toLowerCase());
      if(m.matches())
         return true;
      else
        return false;
}

URL should be in lowercase to validate correctly. Here is ref link.

Hope this helps somebody…

Cheers…