Multiple value insertion – Firebase – Android

In this tutorial, we will learn how to insert multiple data into your firebase database. If you haven’t seen my last article, I would suggest you please go through my last article where I have discussed how to insert and retrieve values from firebase database.


What we are going to do here is we are going to see how we can insert multiple values at once with a simple example project. If you have followed my first two articles (Registration and Login) on firebase then you don’t need to change any rules of your database in the firebase console.

I am mentioning this here because by default firebase database read and write operations are restricted only to the authenticated user and we are sticking with that rule for security purpose.

If you already know how to do authentication and you are here only to see how to insert multiple values at once then just change the rules to public. This way anyone can read and write to your database and you don’t need authentication for that.

If you don’t know how to change the rule so that anyone can have access to your firebase database, click Here.

Now go to Android Studio, create an activity and name it MultipleInsertionActivity or whatever you want. The layout file of this activity will have three EditTexts and one Button. Clicking on the button all the user inputs from three EditTexts will be inserted into your firebase database.

Below is the code of our activity_multiple_insertion.xml file.

And in the MultipleInsertionActivity, at first get the current user. Then on clicking Save button, check if the current user is null. If the current user is not null then insert the values into your firebase database.

Note – If you are not authenticating and your firebase database security rules are public, then you don’t have to check if the user is null or not. In that case, if you do so you will always get the current user null. Since you are not authenticating.

This is our MultipleInsertionActivity code looks like –

Now on the click of Save button, we have to insert all the three values at once. For that, we will HashMap. We know that a HashMap can be used to store key and value pairs.

As you can see, we will have three keys in the database. first_name, middle_name, and last_name and their corresponding values will be the first name, middle name, and the last name. See the image below to understand how our database structure will look like.


As you can see we will also make a child called “user” and the value of the child “user” will be the HashMap objects. Which also has three children first_name, middle_name and last_name.

So take the root URL of your database and add a child “user”.

Don’t forget to replace the URL with yours.

Below is our complete MultipleInsertionActivity code – 

That’s all. Now run your project. Fill three EditTexts with your desired texts and click Save. If everything is good you will see a toast saying “User data successfully saved.”. After saving check your firebase database. You will see your saved user data under the user child.

Still, have doubts? Comment below in the comment section. We will definitely try to help you.


You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *