AutocompletePrediction: using New Places SDK Client

Jul 6 · 3 min read
Photo by Émile Perron on Unsplash

The Google Play Services Places SDK is now deprecated and it has been replaced by the new Places SDK Client. This leaves the android dev to make changes to their existing code with the new implementation. So this article is about the AutocompletePrediction of the places api.

Mr android on Google Map

In android everything starts with the gradle, so the magical lines to add which can provide access to the new places sdk classes and interfaces.

implementation 'com.google.android.libraries.places:places:1.1.0'

And once the gradle build is successful, now its turn to the adapter that has to be written to use the autocomplete feature of the places sdk.

PlaceAutoCompleteAdapter Code

So, now once this PlaceAutoComplete Adapter is created. Now it is time for realizing this adapter where it is required. As the constructor of this adapter reflects, there are parameters that is required for initializing this class, they are:-

1.)Context:- The class android.content.Context provides the connection to the Android system and the resources of the project. So basically if you are in activity you can pass MainActivity.this or if you are in fragment you can use getActivity.

2.) AutocompleteSessionToken:-Session tokens group the query and selection phases of a user autocomplete search into a discrete session for billing purposes. So basically it is a mandatory variables which is related to the billing of your google map api key. And a point that has to be focused is:- “ The session begins when the user starts typing a query, and concludes when they select a place and a call to Place Details is made. If the user does not make a selection, the session will end after a short time out period. Each session can have multiple queries, followed by one place selection. Once a session has concluded, the token is no longer valid; your app must generate a fresh token for each session. We recommend using session tokens for all autocomplete sessions. If you reuse a session token, the session is considered invalid and the requests are charged as if no session token was provided.

AutocompleteSessionToken autocompleteSessionToken; autocompleteSessionToken=AutocompleteSessionToken.newInstance();

3.) PlacesClient:- Client that exposes the Places API methods. Intializing placesclient is done by the method, createClient(Context) method. So is goes as follows:-

PlacesClient placesClient;
placesClient=Places.createClient(getApplicationContext());

Now, the placeAutocompleteAdapterNew can be initialized

PlaceAutocompleteAdapterNew mAdapter;
mAdapter = new PlaceAutocompleteAdapterNew(this, placesClient,autocompleteSessionToken);

And now its done, the only thing that has to be done is now, to pass this adapter to autoCompleteTextView, which is a easy task.

autoCompleteTextView.setAdapter(mAdapter);

So, basically its done now you can enjoy this feature of manual listed places suggestion from the new places sdk. And the migration becomes quite simple.

Happy Coding Guys..!!!

Abhishek Raj Pathak

Written by

Learning and Sharing Android Development

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade