You are on page 1of 9

19/06/2021 How to Change Password of User in Android using Firebase?

- GeeksforGeeks

Build, deploy and scale apps quickly using App GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution. HIDE AD • AD VIA BUYSELLADS

How to Change Password of User in Android using


Firebase?
Difficulty Level :
Hard ● Last Updated :
12 Mar, 2021

In many apps, we got a feature to login using our email and password. Sometimes it

happens that we forget the password and most of the time there reset our password.

Here we are going to implement the same feature to Reset our password using

Firebase Authentication. You may refer to the following ar ticle User authentication

using Firebase in Android. A sample video is given below to get an idea about what we

are going to do in this ar ticle. Note that we are going to implement this project using

the Java language. 

00:00 00:57

Step by Step Implementation

Step 1: Working with the UI par t

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
Go to the activity_login.xml file and refer to the following code. Below is the code for
you have read and understood our
Cookie Policy &
Privacy Policy


the activity_login.xml file.
Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 1/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution.
XML

HIDE AD • AD VIA BUYSELLADS


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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"
    android:orientation="vertical"
    tools:context=".LoginActivity">
  
Related Articles
    <include
        android:id="@+id/login_toolbar"
        layout="@layout/app_bar_layout" />
  
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="19dp"
        android:layout_marginTop="96dp"
        android:textSize="25dp"
        android:text="Login to Your Account" />
  
    <EditText
        android:id="@+id/logemail"
        android:layout_width="267dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="36dp"
        android:layout_marginTop="198dp"
        android:ems="10"
        android:hint="Email"
        android:inputType="textEmailAddress" />
  
    <EditText
        android:id="@+id/logpass"
        android:layout_width="259dp"
        android:layout_height="58dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="32dp"
        android:layout_marginTop="261dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />
  use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
We
    <TextView you have read and understood our
Cookie Policy &
Privacy Policy
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 2/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App


        android:id="@+id/forgetpass"
        android:layout_alignParentStart="true"
GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution.
        android:layout_alignParentTop="true"
        android:layout_marginStart="132dp" HIDE AD • AD VIA BUYSELLADS
        android:layout_marginTop="351dp"
        android:text="Forget Password"/>
    <Button
        android:id="@+id/logbut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="222dp"
        android:layout_marginTop="387dp"
        android:background="@color/colorPrimary"
        android:text="Login"
        android:textSize="15dp" />
  
</RelativeLayout>

Step 2: Working with the LoginActivity.java file

Go to the LoginActivity.java file and refer to the following code. Below is the code for

the LoginActivity.java file 

Java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
  
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
  
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
import com.google.android.gms.tasks.OnCompleteListener;
you have read and understood our
Cookie Policy &
Privacy Policy
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 3/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App


import com.google.firebase.auth.FirebaseAuth;
  
GET $100 FREE CREDIT
  
Platform, DigitalOcean's fully managed solution.
public class LoginActivity extends AppCompatActivity {
HIDE AD • AD VIA BUYSELLADS
    private EditText memail;
    private EditText mpass;
    private FirebaseAuth mAuth;
    private Toolbar mtoolbar;
    private Button login;
    TextView forgetpass;
    public ProgressDialog loginprogress;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        mtoolbar=(Toolbar)findViewById(R.id.login_toolbar);
        setSupportActionBar(mtoolbar);
        
        mAuth = FirebaseAuth.getInstance();
        getSupportActionBar().setTitle("Login");
        
        forgetpass=findViewById(R.id.forgetpass);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        loginprogress=new ProgressDialog(this);
        memail=(EditText)findViewById(R.id.logemail);
        mpass=(EditText)findViewById(R.id.logpass);
        
        // click on forget password text 
        forgetpass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRecoverPasswordDialog();
            }
        });
        
        login=(Button)findViewById(R.id.logbut);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email=memail.getText().toString();
                String password =mpass.getText().toString();
                if(!TextUtils.isEmpty(email)||!TextUtils.isEmpty(password))
                    loginprogress.setTitle("Logging In");
                    loginprogress.setMessage("Please Wait ");
                    loginprogress.setCanceledOnTouchOutside(false);
                    loginprogress.show();
                    loginUser(email,password);
                }
            }
        });
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
    }
you have read and understood our
Cookie Policy &
Privacy Policy
    
    ProgressDialog loadingBar;
    
Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 4/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App


    private void showRecoverPasswordDialog() {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution.
        builder.setTitle("Recover Password");
        LinearLayout linearLayout=new LinearLayout(this); HIDE AD • AD VIA BUYSELLADS
        final EditText emailet= new EditText(this);
          
        // write the email using which you registered
        emailet.setText("Email");
        emailet.setMinEms(16);
        emailet.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        linearLayout.addView(emailet);
        linearLayout.setPadding(10,10,10,10);
        builder.setView(linearLayout);
          
        // Click on Recover and a email will be sent to your registered ema
        builder.setPositiveButton("Recover", new DialogInterface.OnClickLis
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String emaill=emailet.getText().toString().trim();
                beginRecovery(emaill);
            }
        });
        
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickList
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
    
    private void beginRecovery(String emaill) {
        loadingBar=new ProgressDialog(this);
        loadingBar.setMessage("Sending Email....");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();
          
        // calling sendPasswordResetEmail
        // open your email and write the new 
        // password and then you can login
        mAuth.sendPasswordResetEmail(emaill).addOnCompleteListener(new OnCo
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                loadingBar.dismiss();
                if(task.isSuccessful())
                { 
                    // if isSuccessful then done messgae will be shown 
                    // and you can change the password
                    Toast.makeText(LoginActivity.this,"Done sent",Toast.LEN
                }
                else {
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
                    Toast.makeText(LoginActivity.this,"Error Occured",Toast
you have read and understood our
Cookie Policy &
Privacy Policy
                }
            } Got It !
        }).addOnFailureListener(new OnFailureListener() {

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 5/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App


            @Override
            public void onFailure(@NonNull Exception e) {
GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution.
                loadingBar.dismiss();
HIDE AD • AD VIA BUYSELLADS
                Toast.makeText(LoginActivity.this,"Error Failed",Toast.LENG
            }
        });
    }
    public void loginUser(String email,String password){
        mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListe
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    loginprogress.dismiss();
                    Intent mainIntent = new Intent(LoginActivity.this,MainA
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Inten
                    startActivity(mainIntent);
                    finish();
                } else {
                    loginprogress.hide();
                    Toast.makeText(LoginActivity.this,"Cannot Sign In..Plae
                }
            }
        });
    }
}

Output :

00:00 00:57

GitHub link : https://github.com/Anni1123/LoginDemo

Attention reader! Don’t stop learning now. Get hold of all the impor tant Java
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
Foundation and Collectionsyou have readconcepts
and understood our
Cookie
with Policy &
Privacy Policy
the Fundamentals of Java and Java

Got It !

Collections Course at a student-friendly price and become industr y ready. To complete

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 6/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App


your preparation from learning a language to DS Algo and many more,  please refer

GET $100 FREE CREDIT


Platform, DigitalOcean's fully managed solution.
Complete Inter view Preparation Course.

HIDE AD • AD VIA BUYSELLADS

Like 0

Previous Next

ADVERTISEMENT BY ADRECOVER

RECOMMENDED ARTICLES Page : 1 2 3

How to Delete a Firebase User from Adding Firebase to Android App


01 05
Android App? 28, Aug 18

10, Mar 21

How to use Firebase UI


User authentication using Firebase 06 Authentication Library in Android?
02
We in Android
use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
04, Jan 21
you have read and understood our
Cookie Policy &
Privacy Policy
07, Nov 19

Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 7/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

How to Update/Change Email from Text Detector in Android using


Build, deploy
Firebase in Android?and scale apps quickly using
07 Firebase App ML KitGET $100 FREE CREDIT
03
19, Mar 21
Platform, DigitalOcean's fully managed solution. 12, Feb 21

HIDE AD • AD VIA BUYSELLADS

How to Add Firebase Analytics to How to Label Image in Android


04 08
Android App in Android Studio? using Firebase ML Kit?
13, Jan 21 22, Feb 21

Ar ticle Contributed By :

annianni
@annianni

Vote for difficulty

Current difficulty :
Hard

Easy Normal Medium Hard Expert

Article Tags : Firebase, Android, Java

Practice Tags : Java, Android

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
ADVERTISEMENT BY ADRECOVER you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 8/9
19/06/2021 How to Change Password of User in Android using Firebase? - GeeksforGeeks

Build, deploy and scale apps quickly using App GET $100 FREE CREDIT
Platform, DigitalOcean's fully managed solution. HIDE AD • AD VIA BUYSELLADS

5th Floor, A-118,

Sector-136, Noida, Uttar Pradesh - 201305


feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS
Subjects
Copyright Policy Video Tutorials

Practice Contribute
Courses Write an Article
Company-wise Write Interview
Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/how-to-change-password-of-user-in-android-using-firebase/ 9/9

You might also like