Add a Place Autocomplete widget to your Android app

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Let's face facts. Google Autocomplete has totally spoiled us. The predictions that Google presents when typing into google.com are often so good, I can save my delicate fingers from a whole bunch of keystrokes. Now, I actually get annoyed when I encounter any form input field where I have to type the entire text. This especially goes for when I'm entering in my address to order a delivery or search for a nearby business. Well, your users don't have to suffer through this. Have you ever noticed how Google Maps does a place-specific autocomplete when you're typing into the search bar? Through Google Maps Platform, developers can build this Place Autocomplete functionality into any input text field to make it a search bar that helpfully provides on-the-fly predictions for your users as they type. This can be helpful not only to reduce keystrokes for your users, but also to ensure that accurate addresses get entered into your site or app. I'm going to show you how to add this to an Android app with the Places SDK for Android. First off, you'll need to install the SDK in your app-level build.gradle file. Then in your app source code, initialize the client with your API key. The easiest way to add a Place Autocomplete widget is to add the AutocompleteSupportFragment. It's just three steps. First, in the XML layout of your app, add a new fragment called AutocompleteSupportFragment from the Places SDK. This gives you a working text field that displays predictions as the user types. Second, in the corresponding activity source code, initialize the AutoCompleteSupportFragment and associate it with a layout element. Then specify what kind of place your user will expect to type in, and add location biasing by country and geographic coordinates to improve the predictions. This is also a good time to specify which Place Details fields you will need about the selected place so that you'll only pay for data you will actually use. Third, add a PlaceSelectionListener that handles the event when the user taps one of the predictions. And there you go. You've got a fully functional Place Autocomplete widget. If you want your app to use a different navigational flow, for example, to trigger the Autocomplete experience from an icon rather than a search field, your app can launch Autocomplete by using an intent. There are just two steps to this. First, use Autocomplete.IntentBuilder to create an intent, passing the desired Autocomplete mode. When using an intent to launch the Autocomplete widget, you can choose from overlay or full-screen display modes. Just like with the AutocompleteSupportFragment, you'll want to specify any location bias, place type filtering, and Place Details fields that you need. The intent must call startActivityForResult passing in a request code that identifies the intent. Second, override the onActivityResult callback to receive the selected place. Once again, we have a nice Autocomplete widget that we can launch from any navigational element. I've shown you two simple ways to add Place Autocomplete to your Android app. But you may want a finer control over the experience. If you just want the predictions without having Google provide any UI elements, you can use the findAutocompletePredictions method to retrieve predictions programmatically. Oh, thank you. Now you have the power of Autocomplete for your place search input fields. And I have a new T-shirt. I'll be wearing this in some more videos about Place Autocomplete. So please subscribe to get additional tips. Comment below to tell us what else you'd like to see in future Geocasts. Thanks for watching and happy coding.
Info
Channel: Google Maps Platform
Views: 26,694
Rating: undefined out of 5
Keywords: Place Autocomplete, maps API, place autofill, place search field, place autocomplete android, angela yu, place search bar, address autocomplete, Autocomplete API, address autofill, Maps, Place Autocomplete in Places SDK for Android, GDS: Yes;, Google Maps Platform, Angela Yu, Google Maps 101, Google, user prediction, Android, autocomplete in maps, map search, autocomplete android, SDK, autocomplete widget, Geocasts, search bar in maps, place search, search bar in your app
Id: nDC-frkH_LA
Channel Id: undefined
Length: 4min 6sec (246 seconds)
Published: Fri Mar 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.