Read a Google spreadsheet from an Android app and list out that data?

I am working on a Android app to read/write data on a google sheet. I am okay with the write part but read operation only displays a blank screen with no error thrown. It would be helpful if there is a "latest" fine working android program to read data from a google sheet.

asked Mar 15, 2020 at 6:28 453 5 5 silver badges 16 16 bronze badges

Google has documentation for Sheets API v4. There is a Java Quickstart page and Reading & Writing guide. If that does not help, please post some code and the error message (if any).

Commented Mar 15, 2020 at 6:38 I will take a look, Tigger. Commented Mar 15, 2020 at 7:05 How to call the range names in the program? Commented Mar 15, 2020 at 7:48

After a glance on Google Api v4 to read a Google sheet, I am stuck with the part of calling the range in the program. [ // Range names ] part in the code above. Appreciate help! - [ I am a newt to Android programming ]. Is there any working code for reading a google spreadsheet using api v4?

Commented Mar 15, 2020 at 10:12

Finally, after a huge time spent, I fixed the "read" operation of Google sheet. Below is the code which I worked on.

Commented Mar 15, 2020 at 13:15

1 Answer 1

import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.RetryPolicy; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject import java.util.ArrayList; import java.util.HashMap; public class ListofCars extends AppCompatActivity implements AdapterView.OnItemClickListener < ListView listView; SimpleAdapter adapter; ProgressDialog loading; @Override protected void onCreate(@Nullable Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.carslist); listView = findViewById(R.id.listof_cars); listView.setOnItemClickListener(this); getItems(); >private void getItems() < loading = ProgressDialog.show(this, "Loading", "please wait", false, true); StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://script.google.com/macros/s/AKfycbxOLElujQcy1- ZUer1KgEvK16gkTLUqYftApjNCM_IRTL3HSuDk/exec? new Response.Listener() < @Override public void onResponse(String response) < parseItems(response); >>, new Response.ErrorListener() < @Override public void onErrorResponse(VolleyError error) < >> ); int socketTimeOut = 50000; RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); stringRequest.setRetryPolicy(policy); RequestQueue queue = Volley.newRequestQueue(this); queue.add(stringRequest); > void parseItems(String jsonResponse) < ArrayList> list = new ArrayList<>(); try < JSONObject jobj = new JSONObject(jsonResponse); JSONArray jarray = jobj.getJSONArray("listofcars"); for (int i = 0; i < jarray.length(); i++) < JSONObject jo = jarray.getJSONObject(i); String car = jo.getString("car"); String type = jo.getString("type"); HashMapitem = new HashMap<>(); item.put("car", car); item.put("type", type); list.add(item); > > catch (JSONException e) < e.printStackTrace(); >adapter = new SimpleAdapter(this,list, R.layout.list_item_row, new String[],new int[]); listView.setAdapter(adapter); loading.dismiss(); > public void onItemClick(@Nullable AdapterView parent, @Nullable View view, int position, long id) < Intent intent = new Intent(this, Cardetails.class); HashMapmap =(HashMap)parent.getItemAtPosition(position); String car = map.get("car"); String type = map.get("type"); intent.putExtra("car",car); intent.putExtra("type",type); startActivity(intent); > @Override public void onPointerCaptureChanged(boolean hasCapture) <>> 
answered Mar 15, 2020 at 13:27 453 5 5 silver badges 16 16 bronze badges

Paste your respective [SPREADSHEETID] and [SHEETNAME] in the above code closer to the "GET" keyword. Create corresponding (xml) activity for your app with Textviews. Here the response from the sheet is JSON and the JSON is parsed back to display the values in Android app.

Commented Mar 15, 2020 at 13:37

The columns in the google sheet and the strings constants to which we assign the values[i.e column car to string car] are named a same name for easy understanding.