Firebase Authentication - 7 - Change display name


  1. Firebase setup
  2. Layout
  3. Register and Login
  4. Reset password
  5. Verify email
  6. Change profile picture
  7. Change display name
  8. Change email
  9. Change password
  10. Delete account


Welcome to the seventh part of the Firebase Authentication series! In the previous part I showed you how to update a users profile picture in Android Studio with Firebase Authentication. In this part, I will show you how to change a users display name. A popup dialog will be shown when the users want to change their display name where they will be able to type the new requested name. Go ahead and create a new xml file in res/layout and name it dialog_change_displayname.xml.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:hint="New Display Name..."

This file will only consist of an EditText with the hint New Display Name. It will be inflated in, create that class and put it in the DialogFragments folder.

package com.frogitecture.authenticatedgoose.DialogFragments;

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;


public class ChangeDisplayNameDialog extends DialogFragment {

    public interface ChangeDisplayNameListener {
        void onChangeDisplayName(String displayName);

    public ChangeDisplayNameListener listener;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = requireActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.dialog_change_displayname, null);
        final EditText editText = view.findViewById(;

                .setTitle("Change Display Name")
                .setPositiveButton("Change", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String newDisplayName = editText.getText().toString();
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

        return builder.create();

    public void onAttach(Context context) {

        try {
            listener = (ChangeDisplayNameListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(getActivity().toString() + " must implement the listener you silly goose");

We got an interface, ChangeDisplayNameListener, with the method onChangeDisplayName(String displayName). This method is called when the Change button is clicked, and the argument will come from the EditText we created in the xml file.

I'm not going to show you the whole as I have done in the other parts since it's getting rather big, and we are not adding a lot of code. Let's just focus on the changes now. You can go back to the previous part if you need to see the whole class.

We want to show the dialog we just created when a user clicks on the Change display name option in the toolbar, so we create a new object and of it and call it's show method:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {


            ChangeDisplayNameDialog changeDisplayNameDialog = new ChangeDisplayNameDialog();
  ,"Change Display Name");


To get a reaction from the change button we need to implement the interface we created in the ChangeDisplayNameDialog class.

public class MainActivity extends AppCompatActivity implements
        ChangeDisplayNameDialog.ChangeDisplayNameListener {



This will require us to add the actual method, and it will look like this:

public void onChangeDisplayName(final String displayName) {
    loadingDialog.setMessage("Changing display name...");,"Changing Display Name...");

    UserProfileChangeRequest profileChangeRequest = new UserProfileChangeRequest.Builder()

            .addOnCompleteListener(new OnCompleteListener<Void>() {
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {

The loading dialog is first shown when the change button is clicked, and then create a UserProfileChangeRequest object. This time we tell it that we want to change the display name to the string displayName that is passed as an argument from the dialog before, and we call firebaseUser.updateProfile with this object as an argument.If successful, we update the display name TextView in the main activity.

In the next part I will show you how to make it possible for a user to change the email.


authors profile photo

Articles with similar tags

Firebase Authentication - 8 - Change email

Changing email in Android with Firebase Authentication

By Frogitecture

Firebase Authentication - 9 - Change password

Changing a password in Android with Firebase Authentication

By Frogitecture

Firebase Authentication - 10 - Delete account

Deleting an account in Android with Firebase Authentication

By Frogitecture