Forgot Password Activity in Android Studio Java & Kotlin

Today, we will design a Forgot Password Activity in Android Studio in both Kotlin & Java Android Programming languages. In this article, we are going to use Google Material Design guidelines to design the UI. We will also perform the validation of the inputs. You can use this code as a utility while designing Login Screens in your Apps. At the end of this tutorial, we will achieve the following design with input validations.

Below we are going to create a simple Activity for Forgot Password. It contains an email input field and button. In the below image you can see that in case of a wrong email, a message will appear “Please Enter Valid Email” by clicking on the button.

Forgot Password Activity with validation

If the email is valid, a Toast message will display “Email Send to Register Email Address“ as you can see in the following image

Forgot Password Activity

Import Material Design Library In Android Studio

In this article, we are going to use TextInputLayout from Google Material Design for which we need to import Google Material design library. To import that add the following material design dependency in your app level build.gradle file.

implementation 'com.google.android.material:material:1.1.0'

Forgot Password Activity XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#FFFFFF"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forget Password"
        android:textStyle="bold"
        android:textSize="16sp"
        android:textColor="#000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="24dp"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_heading"
        >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            >

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/layout_email"
                android:layout_width="0dp"
                app:layout_constraintWidth_percent="0.7"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="50dp"
                android:textColorHint="#808080"
                app:layout_constraintTop_toTopOf="parent"
                >
                <EditText
                    android:id="@+id/et_email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Email"
                    android:gravity="center"
                    android:padding="10dp"
                    android:textColor="#000000"
                    android:textColorHint="#808080"
                    android:background="#00000000"
                    android:textSize="15sp"
                    android:inputType="textEmailAddress"
                    />
            </com.google.android.material.textfield.TextInputLayout>


            <Button
                android:id="@+id/bt_forget"
                android:layout_width="0dp"
                android:layout_height="50dp"
                app:layout_constraintTop_toBottomOf="@+id/layout_email"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:text="Continue"
                android:textColor="#FFFFFF"
                android:textSize="15sp"
                android:background="@color/design_default_color_primary"
                android:textAllCaps="false"
                android:layout_marginTop="40dp"
                app:layout_constraintWidth_percent="0.7"
                style="?android:attr/borderlessButtonStyle"
                android:elevation="2dp"
                android:onClick="performForgetPassword"
                />

            <LinearLayout
                android:id="@+id/layout_already_have_account"
                android:layout_width="0dp"
                app:layout_constraintWidth_percent="1.0"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="15dp"
                app:layout_constraintTop_toBottomOf="@+id/bt_forget"
                android:orientation="horizontal"
                android:gravity="center"
                >

                <TextView
                    android:id="@+id/tv_already_have_account"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:text="Remember Password!"
                    />

                <Button
                    android:id="@+id/bt_signup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:textColor="@color/design_default_color_primary"
                    android:textSize="18sp"
                    android:background="@android:color/transparent"
                    android:textAllCaps="false"
                    android:text="Login"
                    android:onClick="goToLogin"

                    />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Forgot Password Activity Kotlin Code

If you are developing your Activity in Kotlin then add the following code in your Activity.kt file. otherwise for JAVA code please scroll down.

package com.example.handyopinion

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Patterns.*
import android.view.View
import android.widget.EditText
import android.widget.Toast
import com.example.handyopinion.R.*
import com.handyopinion.LoginActivity

class MainActivity : AppCompatActivity() {

    lateinit var etEmail: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layout.activity_main)
        viewInitializations()
    }

    fun viewInitializations() {

        etEmail = findViewById(R.id.et_email)

        // To show back button in actionbar
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

    }

    fun validateInput(): Boolean {

        if (etEmail.text.toString().equals("")) {
            etEmail.setError("Please Enter Email")
            return false
        }
        // checking the proper email format
        if (!isEmailValid(etEmail.text.toString())) {
            etEmail.setError("Please Enter Valid Email")
            return false
        }
        return true
    }

    fun isEmailValid(email: String): Boolean {
        return EMAIL_ADDRESS.matcher(email).matches()
    }

    fun performForgetPassword (view: View) {
        if (validateInput()) {

            // Input is valid, here send data to your server


            val email = etEmail.text.toString()

            val intent = Intent(this, EmailVerify::class.java)
            startActivity(intent)

            Toast.makeText(this,"Email send to Register Email Address", Toast.LENGTH_SHORT).show()


            // Here you can call you API

        }
    }


}

Forgot Activity JAVA Code

If you are developing your Activity in JAVA then add the following code in your Activity.java file.

package com.example.handyopinion;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.handyopinion.R;

public class MainActivity extends AppCompatActivity {

    EditText etEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewInitializations();
    }

    void viewInitializations() {
        etEmail = findViewById(R.id.et_email);

        // To show back button in actionbar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    // Checking if the input in form is valid
    boolean validateInput() {

        if (etEmail.getText().toString().equals("")) {
            etEmail.setError("Please Enter Email");
            return false;
        }

        // checking the proper email format
        if (!isEmailValid(etEmail.getText().toString())) {
            etEmail.setError("Please Enter Valid Email");
            return false;
        }


        return true;
    }

    boolean isEmailValid(String email) {
        return Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

    // Hook Click Event

    public void performCodeVerify (View v) {
        if (validateInput()) {

            // Input is valid, here send data to your server

            String email = etEmail.getText().toString();

            Intent intent = new Intent(this, EmailVerify.class);
            startActivity(intent);
            Toast.makeText(this,"Email send to Register Email Address",Toast.LENGTH_SHORT).show();
            // Here you can call you API
            // Check this tutorial to call server api through Google Volley Library https://handyopinion.com

        }
    }

}

As mentioned earlier we are using TextInputLayout from the Google Material design library. And we are embedding our EditText inside TextInputLayout which makes our EditText floating. In our Kotlin/JAVA code, we have a method validateInput(). It checks the format of the Email before actually calling your API on Server. In this article, we didn’t make a server request, for that please read our next article.

That’s it. This is how to design a simple Forgot Activity in Android Studio.

Feel free to ask your questions or give suggestions in the comments section below.

Next Article

1. Reset Password Activity in Android Studio Kotlin & Java
2. Send Data to Server Using Google Volley in Android – Kotlin & Java

Please share this post:
Posts created 31

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: