Android AutoCompleteTextView With Google Place API

In the previous series we have seen tutorial on android simple autocompeletion and custom autocompeletion . In this article,  we will develop an Android application which populates an AutoCompleteTextView with Google Places Autocomplete api. The Place Autocomplete service is a web service from google that returns place predictions in response to an HTTP request , The service can be used to provide autocomplete functionality for text-based searches, by returning places addresses as a user types.

Generating Place API Key

The Google Place api key is available in Google Console Visit the link below and follow the steps to generate key .

First we need to build a project with volley library which i have discussed in my previous tutorial how to Build project with Volley .

XML Layout

Create activity_main.xml file inside layout directory and add AutoCompeletionTextView to it

file : Main_Activity.xml
    tools:context="com.pavan.googleautocompletion.MainActivity" >

        android:text="" />


Application Object

Create a class MyApplication which extends Application ,this application object represents singleton pattern , the purpose of this class is make volley request available  through out of the application
  1. The getInstance method of application class returns application object.
  2. The getReqQueue method returns the RequestQueue object.
  3. Inside addToReqQueue method we are adding calling add() method upon RequestObject and passing request as paramter to it .
  4. The cancelPendingReq is used for cancelling the request.

file :
package com.pavan.googleautocompletion;


public class MyApplication extends Application {

    private RequestQueue mRequestQueue;
    private static MyApplication mInstance;

    public void onCreate() {
        mInstance = this;

    public static synchronized MyApplication getInstance() {
        return mInstance;

    public RequestQueue getReqQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());

        return mRequestQueue;

    public <T> void addToReqQueue(Request<T> req, String tag) {


    public <T> void addToReqQueue(Request<T> req) {


    public void cancelPendingReq(Object tag) {
        if (mRequestQueue != null) {

Main Activity

  1. Create a class MainActivity and extend this class to Activity class and set the content of this activity with above defined XML Layout (activity_main.xml) .
  2. Set addTextChangeListener to the autocompeletionTextView when the user enter text make a request and update the suggestion list .

file :
package com.pavan.googleautocompletion;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    String url;
    private static final String TAG_RESULT = "predictions";
    JSONObject json;

    AutoCompleteTextView auto_tv;
    ArrayList<String> names;
    ArrayAdapter<String> adapter;
    String browserKey = "Your API KEY";

    protected void onCreate(Bundle savedInstanceState) {
        auto_tv = (AutoCompleteTextView) findViewById(;

        names = new ArrayList<String>();

        auto_tv.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {


            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {


            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                if (s.toString().length() <= 3) {
                     names = new ArrayList<String>();



    public void updateList(String place) {
        String input = "";

        try {
            input = "input=" + URLEncoder.encode(place, "utf-8");
        } catch (UnsupportedEncodingException e1) {

        String output = "json";
        String parameter = input + "&types=geocode&sensor=true&key="
                + browserKey;

        url = ""
                + output + "?" + parameter;

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
                null, new Response.Listener<JSONObject>() {

                    public void onResponse(JSONObject response) {

                        try {

                            JSONArray ja = response.getJSONArray(TAG_RESULT);

                            for (int i = 0; i < ja.length(); i++) {
                                JSONObject c = ja.getJSONObject(i);
                                String description = c.getString("description");
                                Log.d("description", description);

                            adapter = new ArrayAdapter<String>(
                                    android.R.layout.simple_list_item_1, names) {
                                public View getView(int position,
                                        View convertView, ViewGroup parent) {
                                    View view = super.getView(position,
                                            convertView, parent);
                                    TextView text = (TextView) view
                                    return view;
                        } catch (Exception e) {
                }, new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
        MyApplication.getInstance().addToReqQueue(jsonObjReq, "jreq");


Add Internet Permission Inside Manifest file
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

No comments:

Post a Comment