Android Learning

How to Check Password Pattern in Android

Password Pattern is very necessary when you want to create a secure app system like user management with their data with security. Password Checker or Matcher or Pattern provides a way that how you want to keep your app secure from unauthorized access.

Password Pattern is very necessary when you want to create a secure app system like user management with their data with security. Password Checker or Matcher or Pattern provides a way that how you want to keep your app secure from unauthorized access. Secure App System will provide a reliable connection to the server to your android users. In this tutorial, you will learn the following features: Password Matcher, Password Length, Password Special Character, and Lower and Upper Case and also Numeric Digits. Password Pattern is very useful in the Login and registration system. Using this you can also set Password Length.

How to Create First App in Android Studio Click Here

To Create A Password Strength Checker, Create a new Android Studio Project with empty activity then fill up all the required fields.

Then first of all create a background drawable as shown in the below screenshot:

Right Click on the drawable then select new then choose Drawable Resource File, then choose name as border

then add the below code into this drawable file:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:topLeftRadius="30dp"
        android:topRightRadius="0dp"
        android:bottomRightRadius="30dp"
        android:bottomLeftRadius="0dp" />
    <stroke
        android:width="5dp"
        android:color="@color/purple_500" />
</shape>

Now copy below line of code and paste it in the activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20sp">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Password Checker"
        android:gravity="center_horizontal"
        android:paddingTop="25sp"
        android:textColor="#F40909"
        android:textStyle="bold"
        android:paddingBottom="20sp"
        android:textSize="25sp" />
        <LinearLayout
            android:orientation="vertical"
            android:background="@drawable/border"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20sp">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:freezesText="true"
        android:hint="Enter Password"
        android:clickable="true"
        android:singleLine="true"
        android:textColorHint="@color/black"
        android:textColor="@color/red"
        android:inputType="textPassword"
        android:id="@+id/login_password"
        android:padding="10dp"
        android:background="@null"/>
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:freezesText="true"
                android:hint="Confirm Password"
                android:clickable="true"
                android:singleLine="true"
                android:textColorHint="@color/black"
                android:textColor="@color/red"
                android:inputType="textPassword"
                android:id="@+id/confirm_password"
                android:padding="10dp"
                android:background="@null"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:maxHeight="20dip"
            android:minHeight="20dip"
            android:layout_weight="1"
            android:progress="0"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Weak"
            android:textColor="@color/black"
            android:id="@+id/password_strength" />
    </LinearLayout>
            <Button
                android:id="@+id/loginBtn"
                android:text="login"
                android:textSize="20sp"
                android:background="@drawable/border"
                android:layout_marginTop="20sp"
                android:layout_gravity="center"
                android:layout_width="250dp"
                android:layout_height="60dp"/>
        </LinearLayout>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="10dp"
        android:id="@+id/login_instructions"
        android:text="Your password must be at least 8 characters. With at least 1 number and 1 special character, 1 Upper case and 1 lowercase" />

</LinearLayout>

Now copy the below code and paste it in the MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements TextWatcher {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                EditText password = (EditText)findViewById(R.id.login_password);
                EditText cpassword = (EditText)findViewById(R.id.confirm_password);
                Button loginNow = (Button) findViewById(R.id.loginBtn);
                password.addTextChangedListener(this);
                loginNow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                if(!isValidPassword(password.getText().toString())){
                                        Toast.makeText(MainActivity.this,"Please input valid combination of password!",Toast.LENGTH_LONG).show();
                                } else if((!password.getText().toString().equals(cpassword.getText().toString()))){
                                        Toast.makeText(MainActivity.this, "Confirm Password Not Matched", Toast.LENGTH_SHORT).show();
                                } else if((password.getText().toString().equals(cpassword.getText().toString()))) {
                                        Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                                }
                        }
                });
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
        @Override
        public void beforeTextChanged(
                CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                updatePasswordStrengthView(s.toString());
        }

        private void updatePasswordStrengthView(String password) {

                ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
                TextView strengthView = (TextView) findViewById(R.id.password_strength);
                if (TextView.VISIBLE != strengthView.getVisibility())
                        return;

                if (password.isEmpty()) {
                        strengthView.setText("");
                        progressBar.setProgress(0);
                        return;
                }
                if (isValidPassword(password)){
                        progressBar.setProgress(100);
                        strengthView.setText("Strong");
                } else {
                        progressBar.setProgress(0);
                        strengthView.setText("Weak");
                }
        }
        public boolean isValidPassword(final String password) {
                Pattern pattern;
                Matcher matcher;
                final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$";
                pattern = Pattern.compile(PASSWORD_PATTERN);
                matcher = pattern.matcher(password);
                return matcher.matches();
        }

}

Now you are good to go, run it in virtual or real device to test your app.

From the below download link you can download the Source Code, if you have any doubts or problem then feel free to comment below.

Download

Leave a Comment