Sometimes we have to set some type of form to send the data to our backend this can be any type of form, it can be a contact form or some kind of support form but there is no kind of tool that is provided by Android and other tools require Web View or paid to implement itself so in this article we are going to integrate Google Form in our android app without using any kind of Web View, Design will be ours but the responses will be directly sent to the Google Form at one button click. A sample video is given below to get an idea about what we are going to do in this article.
Step-by-Step Implementation
Step 1: Open Google Form Official Website and Create Your Form add Questions to it.
Step 2: After That Click on Three dots near Your Profile Section And Click On Get Pre-Filled Link.
Step 3: After Clicking on Get Pre Filled Link You’ll get redirected and questions that you added will be displayed So in answer write some unique answers in each so We’ll get a combination of Key-Value Pair that we’ll Use to send Responses To the Form after that click on Get Link Option.
Step 4: After When you have Succesfully Get the Link The Link Will be Visible Like that
now Copy the Keys that We are going to Use
- entry.200562055 is my Name Field
- entry.1045781291 is my Email Field
- entry.1065046570 is my Message Field
Note: Keys Will be Different in Your Case
After that Shorten The Link Like That remove all key value pairs and Change viewForm to formResponse the Final Link Will look like this
Note: Make Sure You Use Your Google Form Link
Step 5: Create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 6: Add Dependencies For Retrofit And Gson , Retrofit Is a Networking Library that We are going to send Our Response and Gson is Used to convert Responses in Kotlin/Java Objects. Open your build.gradle (Module: app) file and add the following lines:
dependencies {
// Other dependencies...
// Retrofit for networking
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
Step 7: 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" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Support Form" android:textSize = "20sp" android:textStyle = "bold" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" android:layout_marginTop = "50dp" app:layout_constraintTop_toTopOf = "parent" /> < EditText android:id = "@+id/nameEt" android:layout_width = "259dp" android:layout_height = "48dp" android:hint = "Enter Your Name" android:textColorHint = "@color/black" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" app:layout_constraintVertical_bias = "0.206" /> < EditText android:id = "@+id/emailEt" android:layout_width = "261dp" android:layout_height = "44dp" android:layout_marginTop = "20dp" android:hint = "Enter Email Address" android:textColorHint = "@color/black" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@id/nameEt" /> < EditText android:id = "@+id/messageEt" android:layout_width = "291dp" android:layout_height = "324dp" android:layout_marginTop = "12dp" android:hint = "Enter Your Message" android:textColorHint = "@color/black" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@id/emailEt" /> < Button android:id = "@+id/btnSubmit" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Submit Form" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintEnd_toEndOf = "parent" android:layout_marginTop = "20dp" app:layout_constraintTop_toBottomOf = "@id/messageEt" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
UI Output:
Step 8: Create a New Interface For Retrofit that Will be Used to Post the Data in Google Form Here We’ll Provide Endpoint and Fields ,Right Click on Your Package Name and Create a New Interface,in my case My Interface Name is GoogleFormApi.
Kotlin
package com.ayush.gfgapp import retrofit2.Call import retrofit2.http.Field import retrofit2.http.FormUrlEncoded import retrofit2.http.POST interface GoogleFormApi { // Method sends form data to the specified Google Form. // // @param name The name of the user. // @param email The email address of the user. // @param message The message from the user. // // @return A Call object that can be used to get the response from the server. @FormUrlEncoded @POST ( "/forms/d/e/1FAIpQLScGXHFDgR2NxnLH55Nut3SJuPYIGvKqnkfLLuphXqVgEOWxNw/formResponse" ) fun sendFormData( @Field ( "entry.2005620554" ) name: String, @Field ( "entry.1045781291" ) email: String, @Field ( "entry.1065046570" ) message: String // if you have more questions you can add more here ): Call<Void> } |
- The @FormUrlEncoded annotation tells Retrofit to encode the form data as URL encoded parameters.
- The @POST annotation tells Retrofit to make a POST request to the specified URL.
- The entry.2005620554 parameter is the name of the field in the Google Form that should be populated with the user’s name.
- The entry.1045781291 parameter is the name of the field in the Google Form that should be populated with the user’s email address.
- The entry.1065046570 parameter is the name of the field in the Google Form that should be populated with the user’s message.
Step 9: Working with MainActivity.kt
Now We’ll Call The Retrofit Builder And Gson To Convert the Response in Kotlin/Java Objects and We’ll add Base Url to it.
Kotlin
package com.ayush.gfgapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.Button import android.widget.EditText import android.widget.Toast import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory class MainActivity : AppCompatActivity() { // Define EditText and Button variables private lateinit var nameEt: EditText private lateinit var emailEt: EditText private lateinit var messageEt: EditText private lateinit var btnSubmit: Button // Create a Retrofit instance for API communication private val retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .build() // Create an API interface using Retrofit private val api = retrofit.create(GoogleFormApi:: class .java) override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) init() // Initialize UI elements btnSubmit.setOnClickListener { // Get user input from EditText fields val name = nameEt.text.toString() val email = emailEt.text.toString() val message = messageEt.text.toString() // Create a call to send form data val call = api.sendFormData(name, email, message) // Execute the API call asynchronously call.enqueue(object : Callback<Void> { override fun onResponse(call: Call<Void>, response: Response<Void>) { try { if (response.isSuccessful) { Toast.makeText( this @MainActivity , "Successful" , Toast.LENGTH_SHORT).show() } else { Toast.makeText( this @MainActivity , "Unsuccessful" , Toast.LENGTH_SHORT).show() } } catch (e: Exception) { Toast.makeText( this @MainActivity , "Error: ${e.message}" , Toast.LENGTH_SHORT).show() } } override fun onFailure(call: Call<Void>, t: Throwable) { try { Toast.makeText( this @MainActivity , "Error: ${t.message}" , Toast.LENGTH_SHORT).show() } catch (e: Exception) { Toast.makeText( this @MainActivity , "Error: ${e.message}" , Toast.LENGTH_SHORT).show() } } }) } } // Initialize UI elements from Your xml private fun init() { nameEt = findViewById(R.id.nameEt) emailEt = findViewById(R.id.emailEt) messageEt = findViewById(R.id.messageEt) btnSubmit = findViewById(R.id.btnSubmit) } } |
Step 10: Add Internet Permission in AndroidMainfest.xml File
<uses-permission android:name="android.permission.INTERNET"/>
Also Open Your Google Form Responses and Go to Settings > Click on Responses and Under Requires Sign In Turn it Off because when it is Turned on then Only Google Signed in User can Send Response.
Output: