Android Firebase Tutorial-Insert Read Firebase data example

How to add setup Firebase configuration inside Android Studio Project and Add Update real time firebase database using EditText.

In this tutorial we would going to insert multiple EditText data into Firebase runtime online database. First we would configure the runtime database on online Firebase server then connect that database to our android app. So here is the complete step by step tutorial for Android Firebase Tutorial-Insert Read Firebase data example tutorial.

Contents in this project Android Firebase Tutorial-Insert Read Firebase data example tutorial :-

  1. Watch the live application demo to know how this app works.
  2. Create a fresh android application in Studio.
  3. Create and configure and connect Firebase database to your Application.
  4. Add internet permission to your project.
  5. Start Coding.

Note :- In source code section i already define each line work using comment so just read the code carefully and you will know exactly how each line works and what work perform each line code.

1. Watch the live application demo to know how this app works :

2. Create a fresh android application in Studio.

3. Create and configure and connect Firebase database to your Application :

Read my this tutorial to connect firebase directly to your android app by importing their libraries and dependencies.

4. Add internet permission to your project :

Open your project’s AndroidManifest.xml file and copy the below permission inside it. You will find the full AndroidManifest.xml file code at the bottom of this page along with all source code.

<uses-permission android:name="android.permission.INTERNET" />

 5. Start Coding for project Android Firebase Tutorial-Insert Read Firebase data example tutorial :

Code for MainActivity.java file.

package com.androidjson.firebase_androidjsoncom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    // Declaring Button object.
    Button SubmitButton, ShowButton;

    // Declaring EditText object.
    EditText NameEditText, PhoneNumberEditText;

    // Declaring String variable ( In which we are storing firebase server URL ).
    public static final String Firebase_Server_URL = "https://fir-ed3b9.firebaseio.com/";

    // Declaring String variables to store name & phone number get from EditText.
    String NameHolder, NumberHolder;

    // // Declaring TextView object.
    TextView ShowDataTextView ;

    // Declaring Firebase object.
    Firebase firebase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Adding MainActivity context into Firebase context.
        Firebase.setAndroidContext(MainActivity.this);

        // Passing firebase Server URL into firebase object.
        firebase = new Firebase(Firebase_Server_URL);

        // Adding ID'S .
        SubmitButton = (Button)findViewById(R.id.submit);

        ShowButton = (Button)findViewById(R.id.show);

        ShowDataTextView = (TextView)findViewById(R.id.showData);

        NameEditText = (EditText)findViewById(R.id.name);

        PhoneNumberEditText = (EditText)findViewById(R.id.phone_number);

        // Adding Click listener to Submit button.
        SubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Declaring student class object.
                Student student = new Student();

                // Calling function to Get data from EditText and store into string variables.
                GetDataFromEditText();

                // Adding student name into student class object.
                student.setStudentName(NameHolder);

                // Adding student number into student class object.
                student.setStudentPhoneNumber(NumberHolder);

                // Passing student phone number and name into firebase object to add into database.
                firebase.child("Student").setValue(student);

                // Showing toast message after data inserted.
                Toast.makeText(MainActivity.this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();

            }
        });

        // Adding click listener to Show data button.
        ShowButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Adding addValueEventListener method on firebase object.
                firebase.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot MainSnapshot) {

                        for (DataSnapshot SubSnapshot : MainSnapshot.getChildren()) {

                            Student student = SubSnapshot.getValue(Student.class);

                            // Adding name and phone number of student into string that is coming from server.
                            String ShowDataString = "Name : "+student.getStudentName()+"\nPhone Number : "+student.getStudentPhoneNumber()+"\n\n";

                            // Apply complete string variable into TextView.
                            ShowDataTextView.setText(ShowDataString);
                        }
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        System.out.println("Data Access Failed" + firebaseError.getMessage());
                    }
                });

            }
        });
    }

    public void GetDataFromEditText(){

        NameHolder = NameEditText.getText().toString().trim();

        NumberHolder = PhoneNumberEditText.getText().toString().trim();

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.androidjson.firebase_androidjsoncom.MainActivity"
    android:layout_margin="7dp">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="Enter Name"
        android:gravity="center"
        android:layout_marginTop="40dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone_number"
        android:hint="Enter Phone Number"
        android:gravity="center"
        android:layout_below="@+id/name"
        android:layout_marginTop="10dp"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/submit"
        android:layout_below="@id/phone_number"
        android:text="Submit Data To Firebase Database"
        android:layout_marginTop="10dp"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/show"
        android:layout_below="@id/submit"
        android:text="Show inserted Firebase data into TextView From Server"
        android:layout_marginTop="10dp"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/showData"
        android:textSize="20dp"
        android:textColor="#000"
        android:layout_below="@+id/show"
        android:layout_margin="20dp"/>


</RelativeLayout>

Code for Student.java file.

package com.androidjson.firebase_androidjsoncom;

/**
 * Created by AndroidJson.com on 5/5/2017.
 */

public class Student {

    private String name;
    private String phoneNumber;

    public Student() {
        // This is default constructor.
    }

    public String getStudentName() {

        return name;
    }

    public void setStudentName(String name) {

        this.name = name;
    }

    public String getStudentPhoneNumber() {
        return phoneNumber;
    }

    public void setStudentPhoneNumber(String phonenumber) {

        this.phoneNumber = phonenumber;
    }

}

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidjson.firebase_androidjsoncom">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshots:

Android Firebase Tutorial-Insert Read Firebase data example tutorial

Download Code

2 Comments

  1. Sir what should i use URL
    insted of your’s

Leave a Reply

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