Implementing Google Forms To Android Application

Google Forms To Android Application

Introduction

In this article, we will see how to create a simple feedback system using Google Forms in the back-end. For online applications, we have a separate table for feedback from users. But, for offline applications like music players, we do not connect any databases like MySQL, SQL Server, etc. We need to build the same with Firebase Real-time Databases also.

Pros of using Google Forms

  1. It provides a real-time user-friendly response
  2. Cost Free
  3. Easy to Integrate.

Coding Part

I have split this write-up into 3 steps as in the following.

  • Step 1- Creating a new project with Empty Activity.
  • Step 2- Setting up the HTTP Library.
  • Step 3- Creating Google Forms.
  • Step 4- Implementation of Google Forms Backend.

Step 1. Creating a new project with Android Studio

  1. Open Android Studio and select "Create New Project".
  2. Name the project as per your wish and select your activity template.

    Google Forms To Android Application

  3. Click the Finish button to create a new project in Android Studio.

Step 2. Setting up the Firebase Library

In this part, we will see how to set up the library for the project.

  1. Then, add the following lines in the app level build.gradle file to apply Google Services to your project.
    dependencies {  
       ...  
       implementation 'com.android.volley:volley:1.1.1'  
    }
  2. Then, click Sync Now to set up your project.

Step 3. Creating Google Forms

  1. The first thing you need to do is to log in to google.com and create a new “Google Form”.
  2. After creating your Google Form, get your share link from the Forms. For Example
    Your link looks like below,
    https://docs.google.com/forms/d/e/1FAIpQLScIpmqndQeQG3lFbj0QkQ1Kt6tEXoPrOt314AZGQ2WKuK8IvA/viewform 
  3. Your shareable link should be converted for posting data to Google Forms. The conversion is very easy. It is just to replace “viewform” in the shareable link to “formResponse”.
    For Example
    After your link will be shown below, 
    https://docs.google.com/forms/d/e/1FAIpQLScIpmqndQeQG3lFbj0QkQ1Kt6tEXoPrOt314AZGQ2WKuK8IvA/formResponse
  4. The tricky part of using Google Forms as Backend is, we have to make a POST request with Key-Value Pair. Values to be posted from the application uses the keys which are to be the IDs of the input fields of Google Forms.
  5. To get the keys of the input fields in the Google Forms, right-click on the textbox and select “Inspect Element”. Take the field’s name from the form which looks like 7374858782.

    Google Forms To Android Application

Step 4. Implementation of Google Forms as Backend

  1. Here, I have used Volley as my HTTP library to make POST request.
  2. Don’t forget to add Internet Permission in AndroidManifest.xml.
  3. Here, I used StringRequest in Volley. The following code will show how the data to be posted to Google Forms.
    public void postData(final String name, final String phone) {  
        StringRequest request = new StringRequest(  
                Request.Method.POST,  
                Constants.url,  
                new Response.Listener<String>() {  
                    @Override  
                    public void onResponse(String response) {  
                        Log.d("TAG", "Response: " + response);  
                        if (response.length() > 0) {  
                            Snackbar.make(fab,"Successfully Posted", Snackbar.LENGTH_LONG).show();  
                            edtName.setText(null);  
                            edtPhone.setText(null);  
                        } else {  
                            Snackbar.make(fab, "Try Again", Snackbar.LENGTH_LONG).show();  
                        }  
                        progressDialog.dismiss();  
                    }  
                }, new Response.ErrorListener() {  
      
            @Override  
            public void onErrorResponse(VolleyError error) {  
                progressDialog.dismiss();  
                Snackbar.make(fab, "Error while Posting Data", Snackbar.LENGTH_LONG).show();  
            }  
        }) {  
            @Override  
            protected Map<String, String> getParams() {  
                Map<String, String> params = new HashMap<>();  
                params.put(Constants.nameField, name);  
                params.put(Constants.phoneField, phone);  
                return params;  
            }  
        };  
        request.setRetryPolicy(new DefaultRetryPolicy(  
                50000,  
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,  
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));  
        queue.add(request);  
    }
  4. You can view the responses in Google form's Response section or in Google sheets. To get response in Google sheet, just click excel symbol in response section of your Google form.

You can read the same on my blog here.

Download Code

You can download the full source code of the article in GitHub. If you like this article, do star the repo in GitHub. Hit like the article.


Similar Articles