Search code examples
androidautocompleteandroid-edittextandroid-search

android Place Autocomplete how to get list of searched place on top by default


I implement place autocomplete in my application and write search keyword in EditText to get result according to search place keyword. here the code for start PlaceAutocomplete activity to search place

int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
try {
Intent intent =
        new 
 PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                .build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
} catch (GooglePlayServicesNotAvailableException e) {
}

when activity launch list showing blank.

enter image description here

What I want- already searched place should be show when activity launch like uber app

enter image description here


Solution

  • You have to persistent your autocomplete results within internal app storage (shared preferences, database..) or somewhere you want. Basically, you need to implement your UI yourself. I don't think you can customise the PlaceAutocomplete.IntentBuilder for your needs.

    1. Implement API-Call for AutocompleteGooglePlaces (we are using retrofit for this)

      @GET("/maps/api/place/autocomplete/json")
      Call<GoogleAutocompleteResponse> getAutoCompleteSearchResults(@Query("key") String apiKey,
                @Query("input") String searchTerm,
                @Query("location") String location,
                @Query("radius") long radius);
      
    2. Perform call (on Enter-click or onTextChange)

      public List<GooglePlaceAutocompleteGeoModel> getAutoCompleteSearchResults(String searchTerm, LatLng center, long radius) {
      
         Call<GoogleAutocompleteResponse> call = googlePlacesRestApiService.getAutoCompleteSearchResults(API_KEY, searchTerm, getLocationStringFrom(center), radius);
      
          try {
              GoogleAutocompleteResponse autocompleteResponse = call.execute().body();
             List<GooglePlaceAutocompleteGeoModel> autocompleteResponsePredictions = autocompleteResponse.getPredictions();
             //Here are your Autocomplete Objects
             return autocompleteResponsePredictions;
      
         } catch (IOException e) {
             L.e("Error on Google Autocomplete call: " + e.getMessage());
         }
      
         return new ArrayList<>();
         }
      
    3. Persistent the result of autocomplete result GoogleAutocompleteResponse within your app and implement logic for showing results in UI or populated within ListView