Adding data to the firebase database:Android app.
create new project in android studio.
First, we have to connect the app with the firebase.
We are connecting our project to the firebase directly from Android studio .
For that in the android studio from the above menu choose the tools option from that click on Firebase option.
On the right-hand side, the assistant menu will pop up from that choose Realtime Database option.
After clicking on that click on "connect your app to firebase" option .
Follow the process.
Next click in "Add Realtime Database to your app".
Accept the changes and complete the process.
It will add some required dependencies in your project .
Now start on actual coding.
First open AndroidManifest.xml file and add permission for the internet as follows:
<uses-permission android:name="android.permission.INTERNET" />
Now build layout for a app.
In our layout, we are having two TextView and one button.
Code for that as follows :
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.hp.realtimedatabase.MainActivity">
<EditText android:id="@+id/editTextTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="2dp" android:layout_marginTop="2dp" android:hint="Title" android:padding="2dp" android:textSize="30dp" />
<EditText android:id="@+id/editTextNotes" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:hint="Notes" android:padding="10dp" />
<Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Save" android:textColor="@color/colorPrimaryDark" />
</android.support.v7.widget.LinearLayoutCompat>
Our layout is ready now open MainActivity.java for java coding.
To enable to use firebase database we have to add two classes from firebase database api for that instance for that as follow :
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mNotesDatabaseReference;
The code in MainActivity.java is as follows :
import android.content.Intent; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap; import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText mTextTitle;
private EditText mTextNotes;
private Button mSave;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mNotesDatabaseReference;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mTextTitle = (EditText) findViewById(R.id.editTextTitle); mTextNotes = (EditText) findViewById(R.id.editTextNotes); mSave = (Button) findViewById(R.id.buttonSave);
/*Retrieve an instance of your database using getInstance()*/ mFirebaseDatabase = FirebaseDatabase.getInstance();
mNotesDatabaseReference = mFirebaseDatabase.getReference().child("Notes");
//Attach clicklistener to button mSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = mTextTitle.getText().toString().trim();
String notes = mTextNotes.getText().toString().trim();
saveNotes(title , notes);
}
});
}
// Save Notes methode
private void saveNotes(String title, String notes) {
/*push creates a unique id in database*/
DatabaseReference newNotesRef = mNotesDatabaseReference.push();
Map noteMap = new HashMap(); noteMap.put("title", title); noteMap.put("notes", notes); newNotesRef.setValue(noteMap); mTextTitle.setText(""); mTextNotes.setText("");
}
}
Coding part is done but this is not complete to start adding data to database we have to change default security options to add our data.
Goto firebase website and goto console and open your project .
Go to database section and open rules tab.
And change that as follows:
{
"rules": {
".read": true,
".write": true
}
}
Save this changes and our app is ready.
But remember this security setting is only for our practice do not apply such rules for production level app if you do it will allow the unauthetificated user to get access to your database.
And one last think this app only add data to database but we can not see that data in our app because we are not implemented data retrieve functionality .
You can check this data in your firebase project console database section.











