Welcome to the eight part of the Firebase Authentication series! In the previous part I showed you how to change a users display name in Android Studio with Firebase Authenticaion. In this part I will show you how to update a users email. It will be pretty similar, a user will click on the option Change email in the toolbar and a popup will show up where the user will be able to type in the new email. Create a new xml file in res/layout and name it dialog_change_email.xml.
dialog_change_email.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/email_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Email..."
android:inputType="textEmailAddress"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"/>
<EditText
android:id="@+id/password_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password..."
android:inputType="textPassword"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
Here we got two EditTexts. One for the new email, and one to confirm the users password. This view is inflated in ChangeEmailDialog.java, go ahead and create that class and put it in the DialogFragments folder.
ChangeEmailDialog.java
package com.frogitecture.authenticatedgoose.DialogFragments;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import com.frogitecture.authenticatedgoose.R;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class ChangeEmailDialog extends DialogFragment {
public interface ChangeEmailListener {
void onChangeEmail(String email, String password);
}
public ChangeEmailListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_change_email, null);
final EditText emailTxt = view.findViewById(R.id.email_txt);
final EditText passwordTxt = view.findViewById(R.id.password_txt);
builder.setView(view)
.setTitle("Change Display Name")
.setPositiveButton("Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String newEmail = emailTxt.getText().toString();
String password = passwordTxt.getText().toString();
listener.onChangeEmail(newEmail, password);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ChangeEmailListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString() + " must implement the listener you silly goose");
}
}
}
We got an interface, ChangeEmailListener
, with the method onChangeEmail(String email, String password)
that is called when a user clicks on the Change button, with the new email and password as arguments grabbed from the EditTexts.
In MainActivity.java, the option Change email becomes alive. When clicked, the dialog is shown:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.change_email:
ChangeEmailDialog changeEmailDialog = new ChangeEmailDialog();
changeEmailDialog.show(getSupportFragmentManager(),"Change Email");
break;
...
}
}
We implement the ChangeEmailListener
so we can make things happen when the change button from that dialog is clicked:
public class MainActivity extends AppCompatActivity implements
ChangeDisplayNameDialog.ChangeDisplayNameListener, ChangeEmailDialog.ChangeEmailListener {
...
}
And the onChangeEmail
method is implemented:
@Override
public void onChangeEmail(final String newEmail, String password) {
loadingDialog.setMessage("Changing email...");
loadingDialog.show(getSupportFragmentManager(),"Changing Email");
String currentEmail = firebaseUser.getEmail();
AuthCredential credential = EmailAuthProvider.getCredential(currentEmail, password);
firebaseUser.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
firebaseUser.updateEmail(newEmail)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
emailTxt.setText(newEmail);
}
loadingDialog.dismiss();
}
});
} else {
Toast.makeText(MainActivity.this, "Authentication failed, wrong password?", Toast.LENGTH_LONG).show();
loadingDialog.dismiss();
}
}
});
}
We fetch the current email with firebaseUser.getEmail()
, match it with the password that was just typed by the user in an AuthCredential
object, and call firebaseUser.reauthenticate
with the credential as an argument. If the user succeeded on typing his password, we proceed to update the email. For this, we just call firebaseUser.updateEmail(newEmail)
. If that too is successful, the email TextView in the main activity is updated to the new email.
In the next part I will show you how to change a users password.
Author
2020-06-22
Changing a display name in Android Studio with firebase auth
Changing a password in Android with Firebase Authentication
Deleting an account in Android with Firebase Authentication