Home » Android » Android : Minimum Length of password in edittext

Android : Minimum Length of password in edittext

Posted by: admin June 15, 2020 Leave a comment

Questions:

In android is there any simple way of setting minimum password length in edittext? In xml there is only an option of max length but not minimum.

Options like setting ems and width fits for restricting the length but what about when to set minimum length???

I found this in the documentation:

DevicePolicyManager mDPM;
ComponentName mDeviceAdminSample;
int pwLength;
...
mDPM.setPasswordMinimumLength(mDeviceAdminSample, pwLength);

But is there any simpler method other than this?

How to&Answers:

You can do some validation on the edittext when the user clicks submit.

public void onSubmitClicked(View v) 
{ 
    String pass = passwordEditText.getText().toString(); 
    if(TextUtils.isEmpty(pass) || pass.length() < [YOUR MIN LENGTH]) 
    { 
        passwordEditText.setError("You must have x characters in your password"); 
        return; 
    }

    //continue processing

}