Android Google Places Autocomplete feature using new Places SDK

A while back, I wrote a blog on Google Place Autocomplete feature. In that blog, I used Google Play Services version of the Places SDK. However, as of January 29, 2019, the Google Play Services version of the Places SDK for Android is deprecated. Google will turn it off by July 29, 2019. Instead of this version of library, Google has now provided a new version of the Places SDK for Android. Idea is, going forward we’ll need to use this new version. I thought why not write a new post using this new version of the library. So, here in this post we’ll learn how to use Android Google Places Autocomplete feature using new Places SDK for Android. In today’s time in almost all the location based mobile application, we get feature to search location manually ourselves for various needs. The new Google Places SDK for Android provides places autocomplete service. It returns predictions of places in response to the search queries entered by the user in the search box. User can keep typing what they want to search, and the autocomplete service keeps returning list of suggestions based on the user input.

There are two ways of adding Google places autocomplete in android application:

In this tutorial, we will cover the first one i.e. by embedding
AutocompleteSupportFragment

App Demo

google-places-auto-complete-new-sdk
google-places-auto-complete-new-sdk

Tech Stack

Android Studio 3.3.2


JAVA for Android programming

Technology stack used in this blog

Create API Key

To get a working API key, you need to complete 3 activities. Get started by clicking in this link -. A new page will open up

  • Pick products (In this case, we’ll click on Places); click on Continue button.
  • Select a project. Here, you’ll see a drop-down list with the projects in your account. Select your project. However, In my case, I’ll select a project that I created previously with the name – ItcGoogleFeaturesDemo. You can create a new project as well if you want so. Click on Next button.
  • Last step is to setup a billing account ( if you don’t already have). Click on Enable billing option and a new page will open up. Setup our billing account here and this wizard will take you to creation of new API key. Note it down.
  • Alternatively, you can also head to this page and follow the official instruction on how to create a new API key

Create an empty project

  • Launch Android Studio and click: File –> New –> New Project…
  • A “Create New Project” dialog box will open; we’ll provide the app name and company domain. I have entered following details, you can provide the name/domain as per your choice and preference.
    • Name:- ItcGooglePlaceAutoCompleteNewPlacesSDK
    • Package Name:- placeautocomplete.iteritory.com
  • I have chosen API 16: Android 4.1 (Jelly Bean) as Minimum API level. Click Finishbutton.
  • I’ll change the app name a bit here, traverse and open res–> values –> strings.xml. Find the line with app_name attribute. Change the value to ““My Google Place App”.

Add dependency

Until now, the Places SDK for Android was made available through Google Play Services. But, the new version of the Places SDK for Android is distributed as a static client library.

  • In app-level build.gradle file, add a dependency for the new SDK client library, as below and then sync the gradle (At the time of writing this blog, following is the latest version. It’s possible that it gets newer version when you are reading. Upgrade the version as such).
    We’ll also add another dependency for a better UI and wrapping the google places autocomplete widget.

implementation ‘com.google.android.libraries.places:places:1.0.0’

implementation ‘com.android.support:cardview-v7:28.0.0’

After adding all the details, the app level build.gradle looks like below –

Add AutocompleteSupportFragment to layout file

Open the layout file (app -> res -> layout -> activity_main.xml). Add the
AutocompleteSupportFragment in the layout file. Also add a textbox to display the user selection. By default, this fragment has no border or background. We will wrap this fragment in a cardview to give a better look. After adding the necessary objects, the layout file looks like –

Add necessary codes to handle the search result

Now that we have added the widget, it’ll show a cardview wrapping the autocomplete widget and also a textbox. So, at this point, if user types, the widget will show related suggestion based on user input. Next, we’ll also add few lines of codes to display what user have actually selected from the suggestion list. To do this, we will add few lines of codes in MainActivity.java. After adding the code, the MainActivity.java looks like below.

NOTE:- You have to replace the YOUR_API_KEY with your own key that you have obtained in the beginning of your tutorial. Also, I have mentioned the key directly in the code and I have done it only demonstration purpose. If you want to implement real-time project, you must secure the key and never mention the API key directly in the code

Conclusion

It was that easy to incorporate Google’s places auto complete feature in your app with just a few line’s of codes. Well, that’s it for today.

203

34 Responses

  1. Trương Huỳnh Hòa
    April 5, 2019
  2. Konstantinos Nikoloutsos
    April 6, 2019
  3. Syeda Sheeba
    April 8, 2019
  4. Ameer Hamza
    April 9, 2019
  5. Muhammad Luqman
    April 10, 2019
    • Sadruddin Md
      April 10, 2019
  6. Muhammad Luqman
    April 12, 2019
  7. prince
    April 17, 2019
  8. Prince
    May 4, 2019
  9. Farooq Ajaz
    May 26, 2019
  10. Jake Lowie Liwag
    June 6, 2019
    • Sadruddin Md
      June 6, 2019
  11. rohith
    June 10, 2019
  12. Max
    June 12, 2019
  13. Diego
    July 19, 2019
  14. Jibu
    July 26, 2019
  15. Brown
    July 30, 2019
    • Sadruddin Md
      July 30, 2019
  16. Shayan Malinda
    August 14, 2019
    • Sadruddin Md
      August 19, 2019
  17. alee
    August 26, 2019
  18. arobit
    November 4, 2019
  19. Ndivho
    November 5, 2019
  20. clinton jb
    June 20, 2020
  21. Sitansu
    November 19, 2020
    • Sadruddin Md
      December 12, 2020
  22. haris
    January 29, 2021
  23. abhirup patra
    February 22, 2021

Write a response

This site uses Akismet to reduce spam. Learn how your comment data is processed.