Using Google Spread Sheet AS DataBase for Android application part-1

While working with some android application, a thought came into my mind that is “Can I use Google Spread Sheet As Back end Source Data Base ? “. Yes after many experiments finally found some useful stuff which is simple to create and use it for simple applications.

 

 

In this article I am going to demonstrate on how to fetch data from Google Spread Sheet, how to parse fetched Data in android application. To understand this experiment one should have good understanding about basic android concepts, list view and adapters, Json parsing.

 

Download Source Code

 

1. Creating Google Spread Sheet URL

(update) :  You can use the below code to create your own script and publish it as web App then follow the steps below 

 

 

Step 1 : Create your spread Sheet with suitable Attributes.

Step 2 : Share your document with option ‘anyone with the link can view’

 

gs1

Step 3 : Goto File -> Publish to web and publish the document.

Step 4 : copy the document id (spread sheet id)

 

gs2

Step 5 : Replace your id in the below URL

 

update : you can use your published(deployed) script url.

 

https://script.google.com/macros/s/AKfycbxOLElujQcy1-ZUer1KgEvK16gkTLUqYftApjNCM_IRTL3HSuDk/exec?id=YOUR_SPREAD_SHEET_ID&sheet=Sheet1

 

Step 6 : Copy the URL and use it JSONparser.java (step 6 of Android part) class as input url

 


2. Android Part

Step 1 : Create a new project in Android Studio from File ⇒ New Project by filling the required details.

Step 2 : Open build.gradle file and add libraries required for this application.

Step 3 : Open layout folder under resoursce and add the below content by creating/modifying activity_main.xml

Step 3.2 : Since we are using list view, copy this code to add elements to invidiual row in the list in layout_row_view.xml

Step 4 : open MainActivity.java file and modify the code as below.

Now the activity is devided into 3 modules

  1. Adapter – to handle list view
  2. Model – represents data format in list
  3. Parser – parses the json data obtained from google sheets.

for all these modules I am creating package and crate the respective class.

 

 

Step 5 : create a package model under main package ( in my example : package androidlabs.gsheets1 ) and create a class MyDataModel.java and add the following code. Make sure that Model matches your defined requirements and make necessary changes if required.

 

Step 6 : create a package parser under main package ( in my example : package androidlabs.gsheets1😉 and create a class JSONparser.java and add the following code. Make sure that Model matches your defined requirements and make necessary changes if required. Here replace url with the of your published google sheet which I will describe in later steps.

Step 7 : create a package adapter under main package ( in my example : package androidlabs.gsheets1😉 and create a class MyArrayAdapter.java and add the following code. Make sure that Adapter matches your defined requirements and make necessary changes if required.

Step 8 :  Now lets optimize the code by adding some utility feature i,.e checking internet connection and adding Keys in order to avoid confusion with the Attributes that are in Google Sheets. So create a Package util and create these 2 classes

 

InternetConnection.java

 

 Keys.java

 

Step 9 : Add or modify the manifest file with the below code

 

Final Output :

Download Source Code