Very good service for Firebase Realtime Database Mobile App. One of your apps is using million users. Just like cricket scores updated. Score updates on that as soon as everyone gets updates right away. Users can also comment on that app again. Comments can be read by all users again. All right with Designing such an app, there are many complex subjects to manage the database. But the complex that uses the Firebase Realtime database is an app or a complex of ideas from any idea can be implemented in one day.

AD: 2

How to use Firebase Realtime Database on Android, we will look at it. First create an Android project. Then copy your bundle identifier. We will create a project from Firebase Console. And add a mobile app under that project. In the firebase article in Google Project, Google has written a detailed description of how to do this. If you have difficulty understanding then you can take it from there. Here we will see how the realtime database can be used.

If you want to use realtime database, you can click on the Database under Projects. We will not have any child for the first time. We’ll make a child. If you move the mouse over the name of the project, you can see it. Click here to add child add options. I gave a name to the child. Like I will store names from my app, so name is named. And value is Jack.

Now come back to the Android project. Compile ‘com.google.firebase: firebase-database: 9.0.1’ should be added between project’s app-level grader’s file dependencies.

There will be a text field in our app, there will be a text editor and there will be a button. You can create a UI 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:

// Read from the database
mRef.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange (DataSnapshot dataSnapshot) {
// This method is called once
// whenever this location is updated
String value = dataSnapshot.getValue (String.class);

}

@Override
public void onCancelled {DatabaseError error} {
// Failed to read value
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);

// Get UI elements

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 running the app, we will not see anything yet. There is a little more left. The rules of the Realtime Database will change from the console. Authentication is required if you want to read data from the default data base or write it. Because we are doing the trial, we will release this authentication for the time being. Let’s justify the read & write as below.

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

Other articles about Android can be found on the Android App Development page. Project source code can be found in Git repository.