How To Use It FireBase Realtime Database Developing. Easy Method 2023

 A very good service for the Firebase Realtime Database mobile app. One of your applications has a million users. Just like cricket scores are updated. Score updates on this as soon as everyone gets updates at once. Users can also comment again on this application. Comments can again be read by all users. It's okay to design such an application, there are many complex topics for database management. But the complex that uses the Firebase Realtime database is an application or a complex of ideas from any idea can be implemented in one day.

How To Use It  FireBase Realtime Database Developing. Easy Method 2023
How To Use It  FireBase Realtime Database Developing. Easy Method 2023 

How to use Firebase Realtime Database on Android, we'll take a look. First create an android project. Then copy your package ID. We will create a project from the Firebase Console. And add a mobile app as part of this project. In the firebase article on Google Project, Google has written a detailed description of how to do this. If you have difficulty understanding, you can take it from there. Here we will see how a real-time database can be used.

If you want to use the live database, you can click on the database in the Projects section. We won't have a baby the first time. We'll make a baby. If you hover your mouse over the name of the project, you can see it. Click here to add options for adding child elements. I named the child. For example, I will be storing names from my application, so the name is called. And the value is Jack.

Now back to the Android project. Compilation "com.google.firebase:firebase-database:9.0.1" should be added between project application level file dependencies.

Our application will have a text box, a text editor, and a button. The user interface can be created as follows.

XML:

<_ id=”@ + id / textViewName” _=”android:_” layout_width=”match_parent” layout_height=”wrap_content” text=”name” textappearance=”? android: attr / textAppearanceLarge” _=”_”>

<_ id=”@ + id / editTextName” _=”android:_” layout_width=”match_parent” layout_height=”wrap_content” hint=”Type Your Name” _=”_”>

<_ id=”@ + id / buttonUpdate” _=”android:_” layout_width=”match_parent” layout_height=”wrap_content” text=”update” _=”_”>

To make data read or write from realtime we need to create a reference. For him:

DatabaseReference mRootRef = FirebaseDatabase.getInstance (). GetReference ();

DatabaseReference mRef = mRootRef.child (“name”);

Here mRootRef.child (“name”); Its name is that we gave the name when we created the child of Firebase, that’s it.

Firebase data is easy to set up

mRef.setValue (“Hello, World!”);

Read the data easily:

mRef.addValueEventListener (new ValueEventListener () {

@Override

public void onDataChange (DataSnapshot dataSnapshot) {

String value = dataSnapshot.getValue (String.class);

}

@Override

public void onCancelled {DatabaseError error} {

Log.w (TAG, “Failed to read value.”, Error.toException ());

}

});

package me.jakir.firebaserealtimedb;

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 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;

public class MainActivity extends AppCompatActivity {

TextView mNameTextView;

EditText mNameEditText;

Button mButtonUpdate;

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();

DatabaseReference mRef = mRootRef.child(“name”);

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mNameTextView = (TextView)findViewById(R.id.textViewName);

mNameEditText = (EditText) findViewById(R.id.editTextName);

mButtonUpdate = (Button)findViewById(R.id.buttonUpdate);

}

@Override

protected void onStart() {

super.onStart();

mRef.addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

String text = dataSnapshot.getValue(String.class);

mNameTextView.setText(text);

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

mButtonUpdate.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mRef.setValue(mNameEditText.getText().toString());

}

});

}

}

After launching the application, we will not see anything yet. There's still a little left. Realtime database rules will be changed from the console. Authentication is required if you want to read or write data from the default database. Since we are running a trial, we will temporarily release this authentication. Let's justify reading and writing as shown below.

Now, after running the app, if we change the value of the child from the console, it will also change to the mobile app. Again, if you write something in the edit text and click "Update", then this application will be updated everywhere as long as the application is installed on it.

More articles about Android can be found on the Android App Development page. The source code for the project can be found in the Git repository.

If want to learn more watch YouTube videos. Thanks for stay with us.

0 Comments