Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Add Data to Back4App Database in Android?

  • Last Updated : 17 Mar, 2021
Geek Week

Prerequisite: How to Connect Android App with Back4App?

Back4App is an online database providing platform that provides us services with which we can manage the data of our app inside the database. This is a series of 4 articles in which we are going to perform the basic CRUD (Create, Read, Update, and Delete) operation with Back4App in Android. We are going to cover the following 4 articles in this series:

  1. How to Add Data to Back4App Database in Android?
  2. How to Read Data from Back4App Database in Android?
  3. How to Update Data in Back4App Database in Android?
  4. How to Delete Data in Back4App Database in Android?

What we are going to build in this article? 

We will be building a simple application in which we will be adding our data to the Back4App database from our Android Application.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Connect your app to Back4App 

We have seen connecting our Android app to Back4App in the prerequisite article. So you can check out that article. 

Step 3: Adding permissions to the internet in the AndroidManifest.xml file

Navigate to the app > AndroidManifest.xml and add the below code to it. 


<uses-permission android:name="android.permission.INTERNET" />

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Edit text for getting course Name-->
        android:hint="Course Name"
        android:inputType="text" />
    <!--Edittext for getting course Duration-->
        android:hint="Course Duration in min"
        android:inputType="time" />
    <!--Edittext for getting course Description-->
        android:hint="Course Description"
        android:inputType="text" />
    <!--Button for adding your course to Firebase-->
        android:text="Submit Course Details"
        android:textAllCaps="false" />

Step 5: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;
public class MainActivity extends AppCompatActivity {
    // creating variables for our edit text
    private EditText courseNameEdt, courseDurationEdt, courseDescriptionEdt;
    // creating variable for button
    private Button submitCourseBtn;
    // creating a strings for storing our values from edittext fields.
    private String courseName, courseDuration, courseDescription;
    protected void onCreate(Bundle savedInstanceState) {
        // initializing our edittext and buttons
        courseNameEdt = findViewById(;
        courseDescriptionEdt = findViewById(;
        courseDurationEdt = findViewById(;
        submitCourseBtn = findViewById(;
        submitCourseBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // getting data from edittext fields.
                courseName = courseNameEdt.getText().toString();
                courseDescription = courseDescriptionEdt.getText().toString();
                courseDuration = courseDurationEdt.getText().toString();
                // validating the text fields if empty or not.
                if (TextUtils.isEmpty(courseName)) {
                    courseNameEdt.setError("Please enter Course Name");
                } else if (TextUtils.isEmpty(courseDescription)) {
                    courseDescriptionEdt.setError("Please enter Course Description");
                } else if (TextUtils.isEmpty(courseDuration)) {
                    courseDurationEdt.setError("Please enter Course Duration");
                } else {
                    // calling method to add data to Firebase Firestore.
                    addDataToDatabase(courseName, courseDescription, courseDuration);
    private void addDataToDatabase(String courseName, String courseDescription, String courseDuration) {
        // Configure Query
        ParseObject courseList = new ParseObject("courses");
        // on below line we are adding our data with their key value in our object.
        courseList.put("courseName", courseName);
        courseList.put("courseDescription", courseDescription);
        courseList.put("courseDuration", courseDuration);
        // after adding all data we are calling a
        // method to save our data in background.
        courseList.saveInBackground(new SaveCallback() {
            public void done(ParseException e) {
                // inside on done method we are checking
                // if the error is null or not.
                if (e == null) {
                    // if the error is null we are displaying a simple toast message.
                    Toast.makeText(MainActivity.this, "Data has been successfully added to Database", Toast.LENGTH_SHORT).show();
                    // on below line we are setting our edit text fields to empty value.
                } else {
                    // if the error is not null we will be
                    // displaying an error message to our user.
                    Toast.makeText(getApplicationContext(), e.getMessage().toString(), Toast.LENGTH_LONG).show();

Now run your app and see the output of the app. You can get to see your data added in the Back4App console. Navigate to Back4App Console and then in the left navbar. You will get to see an option of courses, click on that option you will get to see the data which is added by you. 


Below is the file structure in Android Studio after performing the Add operation:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :