MovieBazar.Info

Latest News & Reviews

FireBase Realtime Database Developing . How To Use It ?

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.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

<_ 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:

1
2
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

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

Read the data easily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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 ());
}
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.

Leave a Reply

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

MovieBazar.Info © 2018