How to Add Data to Back4App Database in Android?
Last Updated :
17 Mar, 2021
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:
- How to Add Data to Back4App Database in Android?
- How to Read Data from Back4App Database in Android?
- How to Update Data in Back4App Database in Android?
- 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.
XML
<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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".MainActivity">
<!--Edit text for getting course Name-->
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Name"
android:importantForAutofill="no"
android:inputType="text" />
<!--Edittext for getting course Duration-->
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Duration in min"
android:importantForAutofill="no"
android:inputType="time" />
<!--Edittext for getting course Description-->
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Description"
android:importantForAutofill="no"
android:inputType="text" />
<!--Button for adding your course to Firebase-->
<Button
android:id="@+id/idBtnSubmitCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Submit Course Details"
android:textAllCaps="false" />
</LinearLayout>
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
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 androidx.appcompat.app.AppCompatActivity;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our edittext and buttons
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
submitCourseBtn = findViewById(R.id.idBtnSubmitCourse);
submitCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
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() {
@Override
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.
courseNameEdt.setText("");
courseDescriptionEdt.setText("");
courseDurationEdt.setText("");
} 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.

Output:
Below is the file structure in Android Studio after performing the Add operation:
Similar Reads
How to Update Data in Back4App Database in Android?
In the previous article, we have seen adding as well as reading data from our Bac4App database in the Android app. In this article, we will take a look at Updating this data in your database. What we are going to build in this article? We will be building a simple application in which we will be u
7 min read
How to Read Data from Back4App Database in Android?
We have seen adding data to the Back4App database in the Android app. In this article, we will take a look at reading this data from our database in our Android App. What we are going to build in this article? We will be creating a new screen in the previous application and inside that, we will dis
9 min read
How to Delete Data in Back4App Database in Android?
We have seen adding data to our Back4App database along with reading and updating this data. In this article, we will take a look at Deleting this data. What we are going to build? We will be adding a simple button inside our update screen where we were updating our data and inside that screen, we
7 min read
How to Create and Add Data to SQLite Database in Android?
SQLite is another data storage available in Android where we can store data in the user's device and can use it any time when required. In this article, we will take a look at creating an SQLite database in the Android app and adding data to that database in the Android app. This is a series of 4 ar
8 min read
How to Install and Add Data to Realm Database in Android?
Realm Database is a service which is provided by MongoDb which is used to store data in users device locally. With the help of this data can be stored easily in users' devices and can be accessed easily. We can use this database to store data in the user's device itself. This is a series of 4 articl
8 min read
How to Update Data to SQLite Database in Android?
We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in
10 min read
How to Update Data in Realm Database in Android?
In previous articles, we have seen adding and reading data from our realm database in Android. In that article, we were adding course details in our database and reading the data in the form of a list. In this article, we will take a look at updating this data in our android app. What we are going
7 min read
How to Read Data from SQLite Database in Android?
In the 1st part of our SQLite database, we have seen How to Create and Add Data to SQLite Database in Android. In that article, we have added data to our SQLite Database. In this article, we will read all this data from the SQLite database and display this data in RecyclerView. What we are going to
12 min read
How to Delete Data in SQLite Database in Android?
In the previous articles, we have seen three operations of CRUD operations such as create, read and update operations in our Android app. In this article, we will take a look at adding delete operation for deleting our items stored in the SQLite database. What we are going to build in this article?
8 min read
How to Read Data from Realm Database in Android?
In the previous article, we have seen adding data to the realm database in Android. In this article, we will take a look at reading this data from our Realm Database in the Android app. What we are going to build in this article? In this article, we will be simply adding a Button to open a new act
8 min read