google places autocomplete android

Google Places Autocomplete Android Example [Updated]

Today we are using google service like google maps, directions and places most of the application. In this tutorial, I am going to explain details about google places autocomplete with examples.

Google places autocomplete search input can be easily implemented using Google API. Google Places API allows you to query for place information on a variety of categories, such as cities, shops and more. A Place Search returns a list of places along with more detailed information about each place.

Google places autocomplete example screenshot

Related Post: How to get current latitude and longitude in android example

Related Post: Glide Android – Image loading framework for Android with Example


First, In order to use Google Places API, you need to configure an API key in Google developers console of your account.

1.Go to Google developers console and log in to your Google account.

2.Create New Project.

create new project in google developer console

3.Go to the credential tab of your Google developer console.

google places autocomplete credential

4.Press edit credential,  Then you can able to see your API KEY. Then Select Android App in the Key Restriction. Then generate Keystore in your android studio and paste it into the package.

add sha1

Now the Google API Key setup Done.

Now Get into the Coding part to implement Google Places Autocomplete.

1. Create New Project in Android Studio.

2. Add the location dependency to your apps build.gradle file.

implementation 'com.google.android.gms:play-services-places:11.8.0'

3. Next, Add the generated API key in your AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.velmurugan.googleautocompleteplacesandroid">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.google.android.geo.API_KEY" android:value="API_KEY "/>
    </application>
</manifest>

4. Add,  The Google PlaceAutocompleteFragment fragment widget which will display the autocomplete fragment in your application.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.velmurugan.googleautocompleteplacesandroid.MainActivity">
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
</LinearLayout>

5. Now you can able see the search box which will show location suggestion based on the keyword user is typing .

Implement the Listener for the Google Places Autocomplete

Use setOnPlaceSelectedListener() and override the onPlaceSelected() Method to perform operation when the user select the Places from the Search list.

placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Toast.makeText(getApplicationContext(),place.getName().toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Status status) {
        Toast.makeText(getApplicationContext(),status.toString(),Toast.LENGTH_SHORT).show();

    }
});

Add Filter in PlaceAutocompleteFragment

In Addition, You can AutocompleteFilter to filter the places which should be listed in suggestion, Like suppose if you want to show just city name instead of an entire address.

placeAutocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder().setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES).build();

placeAutocompleteFragment.setFilter(autocompleteFilter);

placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Toast.makeText(getApplicationContext(),place.getName().toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Status status) {
        Toast.makeText(getApplicationContext(),status.toString(),Toast.LENGTH_SHORT).show();

    }
});

Types of Filter Available for the placeAutocompleteFragment

  • TYPE_FILTER_ADDRESS – Only return geocoding results with a precise address.
  • TYPE_FILTER_CITIES – Return any result matching the following place types: TYPE_LOCALITY, TYPE_ADMINISTRATIVE_AREA_LEVEL_3
  • TYPE_FILTER_ESTABLISHMENT – Only return results that are classified as businesses.
  • TYPE_FILTER_GEOCODE – Only return geocoding results, rather than business results.
  • TYPE_FILTER_NONE – An empty type filter.
  • TYPE_FILTER_REGIONS – Return any result matching the following place types: TYPE_LOCALITY, TYPE_SUBLOCALITY, TYPE_POSTAL_CODE, TYPE_COUNTRY, TYPE_ADMINISTRATIVE_AREA_LEVEL_1, TYPE_ADMINISTRATIVE_AREA_LEVEL_2

Final MainActivity.java

public class MainActivity extends AppCompatActivity {

    private PlaceAutocompleteFragment placeAutocompleteFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        placeAutocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder().setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES).build();

        placeAutocompleteFragment.setFilter(autocompleteFilter);

        placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                Toast.makeText(getApplicationContext(),place.getName().toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Status status) {
                Toast.makeText(getApplicationContext(),status.toString(),Toast.LENGTH_SHORT).show();

            }
        });
    }
}

Related Post: Get Current User Speed Using GPS Android

Related Post: Drag And Drop Listview Android


Download
Download Example Code

6 thoughts on - Google Places Autocomplete Android Example [Updated]

Leave a Reply

Your email address will not be published. Required fields are marked *