In this tutorial, I will show how to create a Reset Password Activity in Kotlin & Java in an easy, adjustable way. In our Activity code, we will also perform validation for the user inputs. At the end of this tutorial, the following screen will be designed.

The user will view 4 digits input fields with the password fields as well. If you input a valid PIN code, password, and reset the password then a Toast message will display “Password Reset Successfully“ as you can see in the following image.
Dependencies
Add the following dependency in your App-level build.gradle file.
implementation 'com.google.android.material:material:1.1.0'
Create Reset Password Activity in Your Project
Now create a new Empty Activity with the name ResetPasswordActivity in your Android Studio. You can create new Activity by right-clicking on your app top folder in the Project panel and then navigate to the following menu.
ResetPassword XML Code
Copy & Paste the following code in your newly created Activity’s XML file.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResetPassword">
<TextView
android:id="@+id/tv_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset 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:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="272dp"
app:layout_constraintTop_toBottomOf="@id/tv_heading"
tools:layout_editor_absoluteX="0dp">
<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/layoutcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColorHint="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_email"
app:layout_constraintWidth_percent="0.7">
<EditText
android:id="@+id/et_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="Pin Code"
android:inputType="phone"
android:maxLength="4"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#808080"
android:textSize="15sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColorHint="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layoutcode"
app:layout_constraintWidth_percent="0.7">
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="Password"
android:inputType="textPassword"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#808080"
android:textSize="15sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_repeat_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColorHint="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_password"
app:layout_constraintWidth_percent="0.7">
<EditText
android:id="@+id/et_repeat_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="Repeat Password"
android:inputType="textPassword"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#808080"
android:textSize="15sp" />
</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_repeat_password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Reset Password"
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="performResetPassword"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Reset Password Activity Kotlin Code
If you are developing your project in Kotlin add the following code in your ResetPasswordActivity.kt file.
package com.example.handyopinion
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
class ResetPassword : AppCompatActivity() {
lateinit var et_code: EditText
lateinit var etPassword:EditText
lateinit var etRepeatPassword:EditText
val MIN_PASSWORD_LENGTH = 6;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reset_password)
viewInitializations()
}
fun viewInitializations() {
et_code = findViewById(R.id.et_code)
etPassword = findViewById(R.id.et_password)
etRepeatPassword = findViewById(R.id.et_repeat_password)
// To show back button in actionbar
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
// Checking if the input in form is valid
fun validateInput(): Boolean {
if (et_code.text.toString().equals("")) {
et_code.setError("Please Enter Valid Code")
return false
}
if (etPassword.text.toString().equals("")) {
etPassword.setError("Please Enter Password")
return false
}
if (etRepeatPassword.text.toString().equals("")) {
etRepeatPassword.setError("Please Enter Repeat Password")
return false
}
// checking minimum password Length
if (etPassword.text.length < MIN_PASSWORD_LENGTH) {
etPassword.setError("Password Length must be more than " + MIN_PASSWORD_LENGTH + "characters")
return false
}
// Checking if repeat password is same
if (!etPassword.text.toString().equals(etRepeatPassword.text.toString())) {
etRepeatPassword.setError("Password does not match")
return false
}
return true
}
// Hook Click Event
fun performResetPassword (view: View) {
if (validateInput()) {
// Input is valid, here send data to your server
val et_code = et_code.text.toString()
val password = etPassword.text.toString()
val repeatPassword = etRepeatPassword.text.toString()
Toast.makeText(this,"Password Reset Successfully",Toast.LENGTH_SHORT).show()
// Here you can call you API
}
}
}
Reset Password Activity Java Code
If you are developing your Project in Java, then add the following code in your ResetPasswordActivity.java file.
package com.example.handyopinion;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class ResetPassword extends AppCompatActivity {
EditText et_code, etPassword, etRepeatPassword;
final int MIN_PASSWORD_LENGTH = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reset_password);
viewInitializations();
}
void viewInitializations() {
et_code = findViewById(R.id.et_code);
etPassword = findViewById(R.id.et_password);
etRepeatPassword = findViewById(R.id.et_repeat_password);
// To show back button in actionbar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// Checking if the input in form is valid
boolean validateInput() {
if (et_code.getText().toString().equals("")) {
et_code.setError("Please Enter Valid Code");
return false;
}
if (etPassword.getText().toString().equals("")) {
etPassword.setError("Please Enter Password");
return false;
}
if (etRepeatPassword.getText().toString().equals("")) {
etRepeatPassword.setError("Please Enter Repeat Password");
return false;
}
// checking minimum password Length
if (etPassword.getText().length() < MIN_PASSWORD_LENGTH) {
etPassword.setError("Password Length must be more than " + MIN_PASSWORD_LENGTH + "characters");
return false;
}
// Checking if repeat password is same
if (!etPassword.getText().toString().equals(etRepeatPassword.getText().toString())) {
etRepeatPassword.setError("Password does not match");
return false;
}
return true;
}
// Hook Click Event
public void performResetPassword (View v) {
if (validateInput()) {
// Input is valid, here send data to your server
String et_code1 = et_code.getText().toString();
String password = etPassword.getText().toString();
String repeatPassword = etRepeatPassword.getText().toString();
Toast.makeText(this,"Password Reset Successfully",Toast.LENGTH_SHORT).show();
// Here you can call you API
}
}
}
Now you can run this project and it should work. This code is Android side code related to the User Interface. We are not calling any API on the server to post data. If you want to make a server call please read our next article.
That’s it. This is how to develop a ResetPassword Activity in Android Studio.
If you have any questions or suggestions, feel free to ask in the comments section below.