How to add – insert values to Firebase android studio tutotrial in 2020

by | Jan 21, 2020 | Android, Android Firebase Tutorials, Database | 0 comments

In this firebase android tutorial you will learn how to connect android with firebase database and get/retrieve/ insert or save data in firebase using android studio. We will add data of the user which he will give using signup form.

So let’s get started.

Get the coamplete Bull’s Rent project.

What is Firebase Database?

Firebase database is a NoSql Realtime Database that let you save data in firebase and retrieve your data in realtime.

Why to use firebase database for android?

As we mentioned earlier firebase is a realtime database and almost all your data will be saved on the cloud. So, to store locally(offline) data of the android app you have to use SQLite database. But to store data online we have to use PHP or other servers, it usually needs a lot of code to store data. So google announced firebase to store online data easily.

1-How to add firebase database in android?

As we have discussed what is the importance of firebase. So to add or connect firebase to our android application you have to follow some steps:

1-first of all, if you don’t add firebase to your project add it.

2-Go to Tools=>Firebase then firebase assistant will open.

3-Select Realtime Database then click on “save and retrieve data“.

4-As a result, new window will open which have some steps related to connection. Just click on the “connect to firebase” button under connect your app to firebase.

5-Furthermore, add a dependency, click on “Add the Realtime Database to your app”.As a result,it will show some dependencies to be added in your app. Then click on accept changes. It will add all the required dependencies to your project.

2-Steps to save data in firebase

Create activity sign up

First of all, we will create a signup material design activity. In which user will fill their data in a sign up screen to save data in the firebase and click the GO button.

If you don’t know, how to make fields in signup screen, likewise below in picture click here

Get code of signup XML activity by clicking and even more

Therefore, we have created material design to sign up XML activity in our previous tutorial. So, now we have to make a “signup.java” activity to save data in the firebase in which users enter on the signup screen.

You guys can get the code of “signup.java activity from the link

1-In “signup.java” activity, first of all, we will make all the variables with two buttons. These are basically text input layout, but not the edit text.

//Variables 
TextInputLayout regName, regUsername, 
regEmail, regPhoneNo, regPassword;
Button regBtn, regToLoginBtn;

2-Furthermore, we will create hooks as you see it in code. If you don’t know how to make hooks. So,  you guys can see aur previous tutorial click here

//Hooks to all xml elements in activity_sign_up.xml 
regName = findViewById(R.id.reg_name);
regUsername = findViewById(R.id.reg_username);
regEmail = findViewById(R.id.reg_email);
regPhoneNo = findViewById(R.id.reg_phoneNo);
regPassword = findViewById(R.id.reg_password);
regBtn = findViewById(R.id.reg_btn);
regToLoginBtn = findViewById(R.id.reg_login_btn);

create database in firebase

3-When the user clicks on the registration button. By clicking on, user data save in firebase. But before that first of all, we have to create a project in firebase. As picture is given below.

 

Therefore, then we have to make database of aur project “buls rent”. Similarly like in the given picture below.

We have to make a realtime database to save data in firebase. Click on create database, a pop-up window appear so you have enabled test mode. Hence you have created your database successfully.

3-When the user clicks on the registration button. By clicking on, user data save in firebase. Furthermore, you guys can get the signup activity by clicking here

In this function, the root node which is a firebase database will call the firebase database root node. The getinstance is calling to the root node, it includes all the data of aur firebase.

rootNode = FirebaseDatabase.getInstance();

4- To access all the user from firebase, we need to define referance.

reference = rootNode.getReference("users");

Register user in firebase

1-First of all, we have to create another activity of “user-helper class”. Once this is created, then we have to add a few variables.  All the variables of user registration such as name, email, phone number, we have to save it in firebase. You guys can get code here.

2-Furthermore, we have to create constructor and getter setter. We can create it by, right click and click on constructor. It create constructor by it’s on.

Similarly, by doing same step, we can make getter and setter.

5-Back to signup screen

Above all, after creating the helper class, back to signup screen. On button click, call over “user helper class”, we have just created. In this constructor, we have to pass all the values which we are getting from aur fields to save data in firebase, just below in code.

String name =regName.getEditText().getText().toString();
String username = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String phoneNo = regPhoneNo.getEditText().getText().toString();
String password = regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, username, email, phoneNo, password);

Show user saved data in firebase

6- Finally, run the application, as you can see in given picture below. All data save in firebase entered by the user.

<?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:background="#fff"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".SignUp">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/bulls_rent_logo_transparent"
        android:transitionName="logo_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-20dp"
        android:fontFamily="@font/bungee"
        android:text="Welcome,"
        android:textColor="#000"
        android:textSize="40sp"
        android:transitionName="logo_text" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SignUp to start your new Journey" android:textSize="18sp" android:transitionName="logo_desc" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/reg_name" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Full Name"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/reg_username" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" android:transitionName="username_tran" app:counterMaxLength="15"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/reg_email" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/reg_phoneNo" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Phone No"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/reg_password" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:transitionName="password_tran" app:passwordToggleEnabled="true"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword"></com.google.android.material.textfield.TextInputEditText> </com.google.android.material.textfield.TextInputLayout> </LinearLayout> <Button android:id="@+id/reg_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000" android:text="GO" android:textColor="#fff" android:transitionName="button_tran" /> <Button android:id="@+id/reg_login_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00000000" android:text="Already have an account? LogIn" /> </LinearLayout>
package com.taimoorsikander.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class SignUp extends AppCompatActivity {
    //Variables 
    TextInputLayout regName, regUsername, regEmail, regPhoneNo, regPassword;
    Button regBtn, regToLoginBtn;
    FirebaseDatabase rootNode;
    DatabaseReference reference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        //Hooks to all xml elements in activity_sign_up.xml 
        regName = findViewById(R.id.reg_name);
        regUsername = findViewById(R.id.reg_username);
        regEmail = findViewById(R.id.reg_email);
        regPhoneNo = findViewById(R.id.reg_phoneNo);
        regPassword = findViewById(R.id.reg_password);
        regBtn = findViewById(R.id.reg_btn);
        regToLoginBtn = findViewById(R.id.reg_login_btn);
        //Save data in FireBase on button click 
        regBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rootNode = FirebaseDatabase.getInstance();
                reference = rootNode.getReference("users");
                //Get all the values 
                String name = regName.getEditText().getText().toString();
                String username = regUsername.getEditText().getText().toString();
                String email = regEmail.getEditText().getText().toString();
                String phoneNo = regPhoneNo.getEditText().getText().toString();
                String password = regPassword.getEditText().getText().toString();
                UserHelperClass helperClass = new UserHelperClass(name, username, email, phoneNo, password);
                reference.child(phoneNo).setValue(helperClass);
            }
        });//Register Button method end 
    }//onCreate Method End 
}
package com.taimoorsikander.myapplication;

public class UserHelperClass {
    String name, username, email, phoneNo, password;

    public UserHelperClass() {
    }
    public UserHelperClass(String name, String username, String email, String phoneNo, String password) {
        this.name = name;
        this.username = username;
        this.email = email;
        this.phoneNo = phoneNo;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Get Complete Source code for Bull’s Rent App

CODING with T

🚀 Supercharge your Flutter skills! Subscribe to my YouTube channel now for mind-blowing coding insights, expert tips, and exclusive content. Don’t miss out!

COURSES

Android firebase app - android app 2022 - firebase android app - bulls rent app - coding with t app - android app
android complete login signup product by taimoor sikander free for any type of android app
Flutter E Commerce App Modern and latest
Flutter Login App UI Source Code - Flutter Complete App - Flutter App Design - Flutter App 2023 - Complete Flutter App 2023
Learn flutter from scratch in 4 hours - Flutter Crash Course - Coding with T

Latest Tutorial

How to create a Custom Appbar in flutter. Custom Appbar Design. Flutter App Design
How to create a Custom Shape in Flutter. Flutter E Commerce app Design. Ecommerce app design Flutter. Flutter clippath tutorial
Bottom Navigation bar in Flutter. Flutter Material 3 bottom navigation bar. How to design background color of Flutter bottom navigation bar. Flutter ecommerce app design
Access Admin Panel, Premium Tutorials, Live Chat, 50% off Codes, and More on Patreon!
This is default text for notification bar