arrow left
Back to Developer Education

Submitting data to a Google form in Android

Submitting data to a Google form in Android

Surveys and questionnaires have been conducted manually for a long time. However, this tends to be tiresome and sometimes does not reach out to many people. <!--more--> Google forms come in handy for conducting surveys as people from different regions can fill them simultaneously. However, these forms can only be accessed via browsers limiting Android and IOS users unless they use mobile browsers.

This article takes care of this by designing a form from the official website and then coming up with an Android application that uses Retrofit to make POST requests to the form to submit data.

Table of contents

Prerequisites

To follow along with this tutorial, you should have:

  • Any IDE for Android development, but Android studio is preferred.
  • Android programming and debugging.
  • Fundamentals of Kotlin.
  • Making network requests with Retrofit in Android.

What is a Google form

Google Forms is a Google product that assists in conducting online surveys efficiently and faster. Different people can fill out the forms, and the responses are sent to one person for analysis.

Creating Google Forms

Follow this link to the official google form website, then select blank form.

blank-form

Create your questions like below:

define-questions

Click on view to see the final form.

final-form

Obtaining entry IDs

The simplest way to obtain the questions entry IDs is; while still on the page you used to create your questions, head over to the top right corner of the page, then on the options menu, select Get pre-filled-link.

pre-filled-link

This will open in a different tab. Fill in all the details as you would have done if you filled out the form, and then click on get Get link.

Copy and paste the generated link somewhere to extract the entry IDs of the different questions in your form. Here is mine :

https://docs.google.com/forms/d/e/1FAIpQLSfY8nzs8rqyBBv4slBUxu8RLKNTe6yYu4lCgmRPY_mrnee0vw/viewform?usp=pp_url&entry.1487586230=Calvin+Omati&entry.167627252=calvombati@gmail.com&entry.2059565087=Male&entry.1673881430=https://github.com/calvinombati&entry.124829766=Kotlin&entry.124829766=Java&entry.124829766=Dart

Our IDs are the parts that start with entry followed by a given number. So, for example, if I break mine down, I will have the following as my final content:

  • entry.1487586230=Calvin+Omati
  • entry.167627252=calvombati@gmail.com
  • entry.2059565087=Male
  • entry.1673881430=https://github.com/calvinombati
  • entry.124829766=Kotlin
  • entry.124829766=Java
  • entry.124829766=Dart

For the checkboxes, you need to get all the IDs of those options as a question can have multiple answers.

Creating an Android Project

Launch your IDE and create a new Android project with a proper name with Kotlin as its primary language.

new-project

Adding the project dependencies

Navigate to your project app-level build.gradle file and add the Retrofit and Gson dependencies.

// Retrofit and Gson
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

Creating the User Interface

We will create a user interface containing several TextViews, Radio Button, and Checkboxes` because we want to test sending answers to several types of questions, i.e. Short text questions, multi-choice questions, and checkboxes.

If you have difficulties coming up with the UI, please check it out in this Github repository Retrofit-GoogleFormDemo.

ui

Take note that I have created the UI and the questions in correspondence with the questions on the google form.

Creating the API service

Create an Interface and create a function to submit the data.

interface ApiService {
    @POST("1FAIpQLSfY8nzs8rqyBBv4slBUxu8RLKNTe6yYu4lCgmRPY_mrnee0vw/formResponse")
    @FormUrlEncoded
    fun submitResponse(
        @Field("entry.167627252") emailAddress: String,
        @Field("entry.1487586230") fullName: String,
        @Field("entry.1673881430") github: String,
        @Field("entry.2059565087") gender: String,
        @Field("entry.124829766") kotlin: String?,
        @Field("entry.124829766") java: String?,
        @Field("entry.124829766") dart: String?,
        @Field("entry.124829766") swift: String?,
    ): Call<Void>
}

The function takes the Retrofit's POST annotation and the endpoint to the form. The BASE_URL is the link to the form as below.

https://docs.google.com/forms/d/e/1FAIpQLSfY8nzs8rqyBBv4slBUxu8RLKNTe6yYu4lCgmRPY_mrnee0vw/viewform

The first part to the "e/" is our BASE_URL. So, for instance, mine is "https://docs.google.com/forms/d/e/".

Our endpoint is from that part to the part that ends with "/viewform". Make sure you append it with a "/formResponse" and remove "viewform"). For instance, the endpoint will be "1FAIpQLSfY8nzs8rqyBBv4slBUxu8RLKNTe6yYu4lCgmRPY_mrnee0vw/formResponse" from my link.

We need to pass the IDs extracted from the form using the @Field annotation to the submitResponse function.

For the questions which had checkboxes, we need to pass all the entry IDs and make them nullable so the user can select none, some, or all.

Go ahead and create an instance of the API service using Retrofit and the BASE_URL that we have extracted. We will use this instance to make the network calls.

You can check out the one I have created in the demo app for reference. - Retrofit-GoogleFormDemo

Submitting data to google form

In the MainActivity inside the onCreate function, we will link the API to the data entered on the form.

We will get the data entered in the EditText fields, the selected gender from the RadioButton, and checked CheckBoxes for favorite languages. Then, we will send the data through the instance of the FormApi.

binding.buttonSubmit.setOnClickListener {

    // Get selected radio button from radioGroup
    val selectedId: Int = binding.radioSex.checkedRadioButtonId

    // Find the radiobutton by returned id
    val gender = (findViewById<RadioButton>(selectedId)).text.toString()

    // CheckBoxes
    val kotlin = binding.kotlinCheckBox.isChecked
    val java = binding.javaCheckBox.isChecked
    val dart = binding.dartCheckBox.isChecked
    val swift = binding.swiftCheckbox.isChecked
            
    FormApi.api.submitResponse(
        binding.edtEmail.text.toString(),
        binding.edtFullName.text.toString(),
        binding.edtGithub.text.toString(),
        gender,
        if (kotlin) {
            "Kotlin"
        } else null,
        if (java) {
            "Java"
        } else null,
        if (dart) {
            "Dart"
        } else null,
        if (swift) {
            "Swift"
        } else null,
    ).enqueue(object : Callback<Void> {

        override fun onResponse(call: retrofit2.Call<Void>, response: Response<Void>) {
            Toast.makeText(
                applicationContext,
                "Form submitted successfully",
                Toast.LENGTH_SHORT
            ).show()
        }

        override fun onFailure(call: retrofit2.Call<Void>, t: Throwable) {
            Toast.makeText(
                applicationContext,
                t.localizedMessage,
                Toast.LENGTH_SHORT
            ).show()
        }

    })
}

Application Demo

Suppose you have followed the steps correctly, then the application should submit the data from the phone to the Google form like below.

demo

When you access your form in edit mode, you should see the response that you just made from your Android phone.

form-response

Conclusion

In this tutorial, we have learned what a Google form is, how to create one, and extract our form questions' entry IDs.

We then created a simple Android application that sends a POST request to the form with the help of Retrofit. Check out this Github repository.

Happy learning!


Peer Review Contributions by: Jerim Kaura

Published on: Apr 7, 2022
Updated on: Jul 15, 2024
CTA

Start your journey with Cloudzilla

With Cloudzilla, apps freely roam across a global cloud with unbeatable simplicity and cost efficiency