Sylvain Saurel
Jul 19 · 4 min read

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 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?

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 a 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 : WEAK, MEDIUM, STRONG 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 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 neither 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 a 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, maximum score a password can have is three.

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

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
  • A 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

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 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 to this enum as background color for the root view. We set the text associated to the enum in the TextView also.

It gives us the following code for the MainActivity class:

Our Password Strength Calculator App in Action!

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 Password Strength Calculator App in Action!

That’s all for that tutorial!

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

Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. Tout sur le Bitcoin : https://www.toutsurlebitcoin.fr. Je Deviens Meilleur : https://www.jedeviensmeilleur.fr

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade