Security has become a major IT challenge. At the heart of this security is the need to force application users to define truly secure passwords. Within an Android application, you may have to offer your users the creation of an account with a login/password. To ensure that they define a sufficiently secure password, I suggest you discover how to develop a Password Strength Calculator Application for Android.

You can also discover this tutorial in video on YouTube:

What defines the strength of a password?

The first step is to define what is the strength of a password. The strength of a password is a rather subjective notion. In the case of our application, we will define it in a rather simplistic way. We will consider 4 levels of strength for a password:

  • Weak
  • Medium
  • Strong
  • Very Strong

We’re going to check four things. So, a password that respects these 4 things will be considered very strong. Similarly, a password that meets 3 of the 4 criteria will be considered strong, and so on.

The last criterion checked will be the length of the password. Thus, to be considered very strong, a password must be longer than 15 characters. In addition, below 8 characters, the password will be considered as having low strength.

Here are the 4 criteria that we will check:

  • Password length
  • Presence of at least one lowercase character and at least one uppercase character
  • Presence of at least one number among the characters of the password
  • Presence of at least one special character

Our criteria are rather simplistic but in the context of a basic Android application, they are perfectly legitimate.

Writing the code for checking the Password Strength

Verifying the strength of a password entered in our application will be done within a dedicated PasswordStrength enum. We use a Java enum with four values: WEAKMEDIUMSTRONG, and VERY_STRONG. For each enum, we associate a String and a color.

The calculation of the strength of a password is implemented within a calculate static method taking a String as input and returning the PasswordStrength enum as output corresponds to the strength of the password entered as a parameter.

So, we iterate on each character contained in the password. First, we search for a special character by checking if a character is neither a letter nor a digit. For that, we use the isLetterOrDigit static method from the Character class. Then, we search for a character which is a digit by calling the isDigit static method from the Character class. And finally, we check if the password contains an upper and a lower character at least. Each time one of these criteria is met, we update the score variable. So, at the end of the iteration, the maximum score a password can have is three.

The last point is got if the password has a length greater than 15 characters. With the score got, we have just to return the correct enum at the end of the method.

It gives the following code for the PasswordStrength enum:

Creating the UI for our Application

The next step is to define the User Interface for our Password Strength Calculator Application. Our UI will be quite simple with the following elements:

  • An EditText for letting users to enter a password
  • TextView for displaying the strength of the password entered

Besides, we will apply the color associated to the PasswordStrength enum returned by the calculate method to the root view of our UI.

It gives us the following code for the activity_main.xml layout file:

Writing the code for the Main Activity

The last step is to write the Java code of the Main Activity. First, we get references for the root view, the EditText, and the TextView. Then, we add a TextWatcher implementation on the EditText for listening to the characters entered by the user. So, each time a character is entered by the user for the password, the onTextChanged method of the TextWatcher implementation is called.

When a character is entered, we calculate directly the strength of the password by passing as input the String entered in the calculate static method of our PasswordStrength enum. Then, we apply the color associated with this enum as the background color for the root view. We set the text associated with the enum in the TextView also.

It gives us the following code for the MainActivity class:

Our Password Strength Calculator App in Action!

The best part of the tutorial is there. We are going to try our Password Strength Calculator Application in action. Once the application is launched, we enter a password and we have the following display:

Our Android password strength application in action

That’s all for that tutorial!

If you have some questions, feel free to use the comments below.