What is Realtime database :
-
It is a cloud hosted database , stores data as JSON and every connected app is synchronised to it always. If we have an cross-platform app, all apps will use one single Realtime Database instance.
-
This database is available offline also. So, application will always has access to it. If the app comes back to online, data is automatically synced again.
-
We can define our security rules to the database.
-
Also we can enable Disk Persistence so that user can access data even if the app loses connection. Once the internet connection is re-established, it will sync the data with Firebase Realtime Database.
In this tutorial, we will use the previous application as it is already configured with Firebase.
If you are not following our tutorials, you can check this tutorial to configure your project .
Gradle dependency :
Add the following dependency to your build.gradle file :
compile 'com.google.firebase:firebase-database:9.2.0'
**Project Setup : **
- We are using the same project from the previous tutorial. Add one Text View,below ”SignUp”, clicking on which will start one new Activity .
<TextView
android:text="Test RealTime Database"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/signUp"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:id="@+id/realTimeDatabase" />
-
Create one new Activity as RealTimeActivity with layout activity_real_time.xml
-
Clicking on ”Test RealTime Database” text will start this new activity.
So, add the below code snippet inside your onCreate() method of MainActivity’s as :
realTimeDataText = (TextView)findViewById(R.id.realTimeDatabase);
realTimeDataText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, RealTimeActivity.class);
startActivity(i);
}
});
- Change your activity_real_time as below :
<?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:id="@+id/activity_real_time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_centerHorizontal="true"
tools:context="com.codevscolor.firebasedemo.realtimedatabase.RealTimeActivity">
<Button
android:text="Add data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:id="@+id/button_add"
android:onClick="addData"
android:layout_gravity="center_horizontal"/>
<Button
android:text="Add More data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_add_more"
android:onClick="addMore"
/>
<Button
android:text="Append data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_append_data"
android:onClick="appendData"
/>
<Button
android:text="Delete data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_delete"
android:onClick="deleteData"
/>
<Button
android:text="Find data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_find"
android:onClick="findData"
/>
<Button
android:text="Update data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_update"
android:onClick="updateData"
android:layout_marginBottom="23dp" />
</LinearLayout>
We have added 6 buttons that will be used to_** add / append/delete/find**_ and update data.
-
Change Permission to read/write data :
1. Go to "database" -> "rules" tab and change as below :
{
"rules": {
".read": true,
".write": true
}
}
Click publish . You can check here about other rules.
2. Start your app and click on "Add data" button.It will call "_**addData**_" method :
public void addData(View view){
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(RealTimeActivity.this, "Oops " + databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
“database” is Firebase Database instance :
private FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_real_time);
database = FirebaseDatabase.getInstance();
}
If you are not getting any error toast msg, that means data is saved to Firebase. If everything works fine, you will see some values inside your “Database” tab:
Here we are using setValue() to save “String” variable to Firebase. We can also save Long, Double, Boolean, Map<String,Object>, List