In this tutorial, we are going to update the data of firebase in the android studio.
Previously, we have designed our profile screen and then store values in the firebase.
Subsequently, we retrieve data from the firebase.
Now we have to update that stored data.
The modification of data that is already present in the database is called an update of data.
Update data is the most important function in any application.
If a human error or any error occurs in saving or storing data, then we have to update that data.
Subscribe us on Youtube
To make an update in the first place we have to get the data from the database and then update it.
Similarly to get the data we have to make an activity the same as profile activity.
So now let’s get started.
Steps to Update data in firebase:
To create a profile screen we have to do the following steps.
- Design an activity
- Make the onClick method.
- Write code in onClick function.
- Update data in firebase.
1- Design an activity for update data in firebase
In the first place, we have to design a new activity to update data.
In this case, we have already designed a profile page in which we retrieve data from the firebase.
Therefore we recommend you to view our tutorials of profile screen design and retrieve data to design the activity to update data.
2- Make onClick Method
Hence after creating the activity and retrieve data from firebase.we have to update data in firebase when the update button clicked.
Thus we have to make an “onClick” function because we want to update data when the update button will click.
So for that, open XML file named as activity_user_profile.xml
Now in the design, click on the update button. go to attributes on the right side of the screen. Search for onClick.
After that write the name of the method as update as shown in the picture.
This will appear as red but don’t be worry, because we haven’t created a method yet.


To remove that error go to UserProfile.java. make a function named as update having attribute of View as shown in the code below.
public void update(View view){
}
Now you will see that the error is gone.
3- Write code in onClick method
As we have already retrieve data from firebase so we will use the same variables for further use.
Now in the update function, we will update data only if the name or password is changed.
You can also add anything you want but in this case, we will only apply the check on name and password changed.
So for that make two functions named as isNameChanged() and isPasswordChanged() with return type Boolean.
4- Update Data in firebase.
To update data in firebase, Above all we have to refer to our firebase database.
Therefore make a global variable of reference. and assign the root to it. In our case, the root is “users” so our code will be.
reference = FirebaseDatabase.getInstance().getReference("users");
Earlier we have created 2 functions to check if the data is changed Now write the code in it.
like that if the name or password changed then data will update otherwise it shows that data is the same. As shown in the code below.
private boolean isPasswordChanged(){
if(!_PASSWORD.equals(password.getEditText().getText().toString()))
{
reference.child(_USERNAME).child("password").setValue(password.getEditText().getText().toString());
_PASSWORD=password.getEditText().getText().toString();
return true;
}else{
return false;
}
}
private boolean isNameChanged(){
if(!_NAME.equals(fullName.getEditText().getText().toString())){
reference.child(_USERNAME).child("name").setValue(fullName.getEditText().getText().toString());
_NAME=fullName.getEditText().getText().toString();
return true;
}else{
return false;
}
}
Get Complete Source code for Bull’s Rent App
To learn in detail just follow our Youtube tutorial..
Android Related Videos
- Design a Modern Dashboard
- Material Design Login Screen with Shared Animations
- Create a Navigation Drawer Material Design in 2020
- Design Responsive Android App to fit all screen sizes
Related Articles that you might want to check out.
- Create a new project in android studio for beginners?
- How to create an Account on Fiverr and start earning?
- Create a Gig on Fiverr and start selling specific services.
- How To Start Blogging and Make Money?
Our Projects Make Us Proud