Android

Download an Android project with source code and start using Back4App

Introduction

In this guide, you will learn how to get started with an Android application written in Java or Kotlin and connect it to Back4App.

If you want a detailed Quickstart guide or connect Back4App to an existing project, go to our Install Parse SDK tutorial

Goal

Download an Android Template and connect it to Back4App

Prerequisites

Step 1 - Download the template

There are 2 Android templates, one written in Java and the other on Kotlin:

Choose the template that suits you, and proceed to download or import your project on Android Studio. Android Studio.

Step 1.1 - Download Directly from GitHub

Use the following commands to download and unzip your project template:

$ curl -LOk https://github.com/templates-back4app/android-java-starter-template/archive/master.zip && unzip master.zip
$ curl -LOk https://github.com/templates-back4app/android-kotlin-starter-template/archive/master.zip && unzip master.zip

Step 1.2 - Open the project on Android Studio

After downloading the files, unzip them. Let’s open Android Studio

In the welcoming screen of Android Studio, choose ‘Open an Existing Project’ and select the project’s folder.

Choose your downloaded and unzipped folder’s location and open it.

Please wait until the finish of the Gradle Run process. Now you can see Gradle console bottom tabs in Android Studio.

Step 1.3 - Import from GitHub(Optional Path)

You can import the repository link directly to Android Studio. On Android Studio welcoming screen, choose ‘Get from Version Control’

Android Studio will ask you for the Git repository link and the desired project path. You can find repository links at the start of this section.

You can find repository links in the start of this section

After filling the URL and Directory fields, click on the Clone button. Then Android Studio will copy and open the project for you. Please wait until the finish of the Gradle Run process. Now you can see Gradle console bottom tabs in Android Studio

Android Studio will copy and open project for you

Please wait until gradle run is finished.You can see gradle console bottom tabs in Android Studio

Step 2 - Get your App Keys

In this guide we will use following files in project :

AndroidManifest.xml - We will set our Back4App credentials as <meta-data> and app permissions
App.java (App.kt for kotlin) - We will modify our initialization code in here
MainActivity.java (MainActivity.kt for kotlin) - Will contain our very first code for creating a Parse Object
strings.xml - We will store and read Back4App setup credentails from here
build.gradle - We will set our Parse Android SDK version in here

In order to connect your App project to Back4App’s server, you’ll need three primary information the server URL, the Application ID, and the Client Key.
In an Android project, strings.xml is a perfect place to set this information. It is where Parse Android SDK reads Application key values to make a connection with your Back4App App.
The server URL is already on the project. You’‘ll need now go to Back4App, copy your App keys, and update your strings.xml with those values:

  1. Open your strings file: .../app/src/main/res/values/strings.xml

  2. Go to your App Dashboard at Back4App Website.

  3. Find you keys on: App Settings > Security & Keys.

  4. Return to your strings.xml file and paste your applicationId and clientKey.

    1
    2
    3
    4
    5
    6
    7
    8
    
     <resources>
         <string name="app_name">Back4AppExample</string>
         <string name="back4app_server_url" translatable="false">https://parseapi.back4app.com/</string>
            
         <!-- Paste BOTH keys here  -->
         <string name="back4app_app_id" translatable="false">PASTE_YOUR_APPLICATION_ID_HERE</string>
         <string name="back4app_client_key" translatable="false">PASTE_YOUR_CLIENT_KEY_HERE</string>
     </resources>
    


  5. Open your build.gradle (Module:Back4AppExample.app) file in Gradle Scripts from Project Explorer

    In dependencies section change the Parse-SDK-Android value with version of your choice.

    1
    
         implementation "com.github.parse-community.Parse-SDK-Android:parse:latest-version-here"
    


    After saving build.gradle run ‘Sync Now’

You can see current version of SDK in here SDK Versions.

Step 3 - Connect to Back4App

After setting up your App credentials, you are ready to connect with your Parse Server instance on Back4App.

This is the initialization code you’re going to use:

You can reach initialization code in project in App.java (App.kt for kotlin)

We are using App.java for our initialization because we need to establish connection before app takes any other action. App.java is the first Context to be created before any other Activity and Service and last to be destroyed.

Below initilization code gets App Keys from strings.xml and try to establish a connection with our Back4App server. We put our code onCreate() method because we want to connect to our server first before taking any other action.

app > java > com > back4app > java > example > App.java

1
2
3
4
5
6
7
8
9
10
11
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(new Parse.Configuration.Builder(this)
                .applicationId(getString(R.string.back4app_app_id))
                .clientKey(getString(R.string.back4app_client_key))
                .server(getString(R.string.back4app_server_url))
                .build());
    }
}

app > java > com > back4app > java > back4appexample > App.kt

1
2
3
4
5
6
7
8
9
10
11
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Parse.initialize(
            Parse.Configuration.Builder(this)
                    .applicationId(getString(R.string.back4app_app_id))
                    .clientKey(getString(R.string.back4app_client_key))
                    .server(getString(R.string.back4app_server_url))
                    .build());
    }
}

Now it is time to add some codes for interacting with the server. Let’s open our MainActivity file.

Activity files are great for interacting with user. They are main purpose providing a User Interface.

You can choose which activity to show in launch in AndroidManifest.xml

1
2
3
4
5
6
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In our project MainActivity is set to open on launch.

In this code sample we have a Parse SDK code for saving a Parse Object to server and showing objectId of saved Parse Object to user with a TextView


app > java > com > back4app > java > example > MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.textView);
        ParseObject firstObject = new  ParseObject("FirstClass");
        firstObject.put("message","Hey ! First message from android. Parse is now connected");
        firstObject.saveInBackground(e -> {
            if (e != null){
                Log.e("MainActivity", e.getLocalizedMessage());
            }else{
                Log.d("MainActivity","Object saved.");
                textView.setText(String.format("Object saved. %s", firstObject.getObjectId()));
            }
        });
    }
}

app > java > com > back4app > kotlin > back4appexample > MainActivity.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val textView = findViewById<TextView>(R.id.textView)
        val firstObject = ParseObject("FirstClass")
        firstObject.put("message","Hey ! First message from android. Parse is now connected")
        firstObject.saveInBackground {
            if (it != null){
                it.localizedMessage?.let { message -> Log.e("MainActivity", message) }
            }else{
                Log.d("MainActivity","Object saved.")
                textView.text = String.format("Object saved. %s", firstObject.objectId)
            }
        }
    }
}

Step 4 - Test the connection

  1. Build your app in a device or virtual device (Shift+F10).

If you don’t have any virtual device to run app. You can create a new one from AVD Manager in Android Studio

  1. Wait until the Hello Word! screen appears. After Hello Word! you will see Object saved. Message this message will include saved object’s id.

  2. Login at Back4App Website.
  3. Find your app and click on Dashboard > Database > Browser.

If everything works properly, you should find a class named FirstClass as follows:

It’s done!

You can see objectId in dashboard and your app’s screen is matches !

At this point, you have learned how to get started with Android apps.

Learn more by walking around our Android Tutorials or check Parse open source documentation for Android SDK.