Save Data Real-Time Database FireBase

Save Data Real-Time Database FireBase

Hi, I am Muhammad Arif! Today! I will teach you in this article How to Save Data Real-Time Database FireBase. You will get the following things in this article.

  • All Steps
  • Source code
  • Practical tutorial video

Step1:

Connect App to Firebase. You can Connect App to Firebase to Two methods. 1) Manual 2) Auto. For more details video Lecture.

Step2:

Create Two edit Text & one Button in an XML file. Here is Main XML file Source Code.

<EditText
   android:id="@+id/name"
   android:layout_marginTop="30dp"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="Enter Full Name"/>

<EditText
    android:id="@+id/email"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Address"/>

<Button
    android:id="@+id/save"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Save data"/>

Step3:

Go to Main Java file Add this Source Code.

public class MainActivity extends AppCompatActivity {

// Create an object or initials

    private EditText name, email;
    private Button save;
    DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
// Get id 
        name = (EditText)findViewById(R.id.name);
        email = (EditText)findViewById(R.id.email);
        save = (Button) findViewById(R.id.save);

databaseReference = FirebaseDatabase.getInstance().getReference().child("users");

// Set Click Listener

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             AddData();
            }
        });
    }

    public void AddData(){
        String Name = name.getText().toString().trim();
        String Email = email.getText().toString().trim();

        SaveData saveData = new SaveData(Name,Email);
        databaseReference.push().setValue(saveData);
Toast.makeText(getApplication(), "Save SuccessFully", Toast.LENGTH_LONG).show();
    }
}

Step4:

Create new JAVA fileName as SaveData.java.

String name, email;
// Add constructor 

public String getName() {
   return name;
}

public void setName(String name) {
   this.name = name;
}

public String getEmail() {
   return email;
}

public void setEmail(String email) {
   this.email = email;
}

public SaveData(String name, String email) {
   this.name = name;
   this.email = email;
}

// Getter and Setter



// Click on package java
public void AddData(){
public void AddData(){
        String Name = name.getText().toString().trim();
        String Email = email.getText().toString().trim();
    }

-----------------Complete Code----------------
package arif.android.firebase;

public class SaveData {

    String name, email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public SaveData(String name, String email) {
        this.name = name;
        this.email = email;
    }
}

Step5:

The last Step is the Change Database Rules:

{

  "rules": {
    ".read": true,
    ".write": true
  }
}
Save Data Real-Time Database FireBase Urdu | Hindi