Breaking News

Creating and Inserting Value in Firebase Realtime Database

Creating and Inserting Value in Firebase Realtime Database




  • Retrieve From database and show it on ListView Click Here
  • Update data in Database Click Here
  • Delete Data in Database Click Here



Firebase Realtime Database is an online database used for the android app and it's free of cost but with some limits.

Today, we create a Firebase Realtime database and Insert some values in it.

  • Create a project in Android Studio.
  • After a successful build, open Tools from the menu and click on Firebase.
Firebase Assistant in Android Studio
Firebase Assistant in Android Studio

  • Click Realtime Database from Assistant
Firebase Realtime Database in Android Studio Assistant

Firebase Realtime Database in Android Studio Assistant

  • Then Connect your Database and Click on Add Realtime Database to add required dependencies in your project
Connect and Add Firebase Dependencies in Android project
Connect and Add Firebase Dependencies in Android project

Then Setup your activity_main.xml

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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=".MainActivity">  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Roll No"  
     android:id="@+id/student_roll"/>  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Name"  
     android:id="@+id/student_name"/>  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:hint="Std"  
     android:id="@+id/student_std"/>  
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Add Student"  
     android:id="@+id/add_btn"/>  
 </LinearLayout>  

Then Create a Model Class

Student_Model.java
 package studio.harpreet.firebasedatabase;  
 import androidx.annotation.Keep;  
 @Keep  // when you want to publish your apk to Google play store, because proguard not add some java files  
 public class StudentModel {  
   String id, rollno,name,std;  
   public StudentModel() {  
   }  
   //Shortcut is alt+insert  
   public StudentModel(String id, String rollno, String name, String std) {  
     this.id = id;  
     this.rollno = rollno;  
     this.name = name;  
     this.std = std;  
   }  
   public String getId() {  
     return id;  
   }  
   public String getRollno() {  
     return rollno;  
   }  
   public String getName() {  
     return name;  
   }  
   public String getStd() {  
     return std;  
   }  
 }  

Then Setup your MainActivity.java

MainActivity.java
 package studio.harpreet.firebasedatabase;  
 import androidx.annotation.NonNull;  
 import androidx.appcompat.app.AlertDialog;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.content.DialogInterface;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.LinearLayout;  
 import android.widget.ListView;  
 import android.widget.TextView;  
 import com.google.firebase.database.DataSnapshot;  
 import com.google.firebase.database.DatabaseError;  
 import com.google.firebase.database.DatabaseReference;  
 import com.google.firebase.database.FirebaseDatabase;  
 import com.google.firebase.database.ValueEventListener;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class MainActivity extends AppCompatActivity {  
   DatabaseReference databasestudent;  
   EditText roll_et,name_et,std_et;  
   Button add_btn;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     roll_et = findViewById(R.id.student_roll);  
     name_et = findViewById(R.id.student_name);  
     std_et = findViewById(R.id.student_std);  
     add_btn = findViewById(R.id.add_btn);  
     databasestudent = FirebaseDatabase.getInstance().getReference("Student");  
     add_btn.setOnClickListener(new View.OnClickListener() {  
                     @Override  
                     public void onClick(View v) {  
                       addstudent(roll_et.getText().toString().trim(),  
                           name_et.getText().toString().trim(),  
                           std_et.getText().toString().trim());  
                     }  
                   });  
   }  
 private void addstudent(String Rollno, String Name, String Std)  
   {  
     String id = databasestudent.push().getKey();  
     StudentModel studentModel = new StudentModel(id,Rollno,Name,Std);  
     databasestudent.child(id).setValue(studentModel);  
   }  
 }  

Then we have to add googleservices.json file which we can download from firebase project settings.

Download googleservices.json file from firebase realtime database
Download googleservices.json file from firebase realtime database

Then change your view from android to project view and paste googleservices.json under the app section.

Add Google Services.json file to Android project
Add Google Services.json file to Android project

Then click on Database from the left menu in Firebase Console

Change your rules to true to add and access values from the database. we will talk about security of rules in our next post.

Set up Rules in Firebase Database
Set up Rules in Firebase Database


Then Finally run your app and add some data to show on your Database Table in Data Tab.



Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

46 comments:

  1. As always as widespread contractors web dsign company, you are in inappropriate hands with those men!

    ReplyDelete
  2. Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next.. création de site internet

    ReplyDelete
  3. very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! WordPress Developer Brisbane

    ReplyDelete
  4. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    토토사이트

    ReplyDelete
  5. I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.
    파워볼사이트

    ReplyDelete
  6. Thank you for some different informative blog. Wherein else may want to I am getting that kind of facts written in such an ideal approach? I have a challenge that I’m simply now operating on, and i have been on the appearance out for such records.
    Roof Restoration Adelaide

    ReplyDelete
  7. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! 먹튀사이트

    ReplyDelete
  8. Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website. i99pro

    ReplyDelete
  9. Great articles and great layout. Your blog post deserves all of the positive feedback it’s been getting. 안전놀이터

    ReplyDelete
  10. This is actually the kind of information I have been trying to find. Thank you for writing this information. cheap hosting in indiaonohosting

    ReplyDelete
  11. https://lianmeiting.blogspot.com/2014/12/studio-sands-pole-dancing-school.html?showComment=1625975529537#c1522390455532264158

    ReplyDelete
  12. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. here

    ReplyDelete
  13. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. 토토사이트

    ReplyDelete
  14. i am always looking for some free stuffs over the internet. there are also some companies which gives free samples. uwatchfree

    ReplyDelete
  15. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! matka chart

    ReplyDelete
  16. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. Watergardens at Canberra Price

    ReplyDelete
  17. I really like your writing style, great information, thankyou for posting. Pollen Collection Price

    ReplyDelete
  18. I can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. 온라인슬롯

    ReplyDelete
  19. Merely a smiling visitant here to share the love (:, btw outstanding style. bolly4u

    ReplyDelete
  20. Cool stuff you have got and you keep update all of us. 안전놀이터

    ReplyDelete
  21. I am continually amazed by the amount of information available on this subject. What you presented was well researched and well worded in order to get your stand on this across to all your readers. 토토사이트

    ReplyDelete
  22. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... 무료릴게임

    ReplyDelete
  23. Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking. pengeluaran sgp

    ReplyDelete
  24. Everything has its value. Thanks for sharing this informative information with us. GOOD works! magicien mariage Lyon

    ReplyDelete
  25. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! pussy888

    ReplyDelete
  26. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. 389sports

    ReplyDelete
  27. Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website. https://sites.google.com/view/hotmail-loginsigup/hotmail-login

    ReplyDelete
  28. This is actually the kind of information I have been trying to find. Thank you for writing this information. Filmygod

    ReplyDelete
  29. Join Public Mobile now with Public Mobile Promo Code E3LRK5 and get $10 off your cell phone bill using a Public Mobile Referral Code exclusively on Newsrooms. Newsrooms

    ReplyDelete
  30. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. buy packwoods marathon Uncle sam OG

    ReplyDelete
  31. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. best payment processing companies to work for

    ReplyDelete
  32. Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website. how to fix dull hair

    ReplyDelete
  33. This is actually the kind of information I have been trying to find. Thank you for writing this information. joker123

    ReplyDelete
  34. Cool stuff you have got and you keep update all of us. sbobet asia

    ReplyDelete
  35. Liposuction is a vacuum cleaner for fat. With unparalleled safety and minimal discomfort, the surgeon uses a pump to suck out unwanted areas of fat. Click Here

    ReplyDelete
  36. Most patients want to have liposuction done by a specialist. A liposuction specialist is someone who performs this procedure almost daily. click here for more

    ReplyDelete
  37. An interesting dialogue is price comment. I feel that it is best to write more on this matter, it may not be a taboo topic however usually individuals are not enough to talk on such topics. To the next. Cheers. 123 movies

    ReplyDelete
  38. Hi there! Nice stuff, do keep me posted when you post again something like this! 안전놀이터

    ReplyDelete
  39. There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way.
    nex777 login
    nex777 login
    nex777 login
    nex777 login
    nex777 login
    sip777 login

    ReplyDelete
  40. thanks for this usefull article, waiting for this article like this again. 팔팔카

    ReplyDelete
  41. This is actually the kind of information I have been trying to find. Thank you for writing this information. บาคาร่าออนไลน์

    ReplyDelete
  42. howdy, your websites are really good. I appreciate your work. 먹튀검증

    ReplyDelete
  43. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. 대전1인샵

    ReplyDelete
  44. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck. 먹튀검증

    ReplyDelete
  45. Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! 토토커뮤니티

    ReplyDelete
  46. A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. 토토커뮤니티

    ReplyDelete