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 

6 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