Can i use the Google places API in an edittext?

Viewed 1930

Is it possible to use the Google places API in this layout:

layout

I highlighted the EditText field where i want to use the API in.

I've already searched for numerous guides on how to do this but it seems all of them use deprecated code. I'm also fairly new to Fragments but i would like to get to know more of this topic.

2 Answers

set onclicklistener on that edittext and in onclick method call below function

private void searchPlace() { 
if (!Places.isInitialized(){ Places.initialize(this, getApplication().getString(R.string.google_maps_key)); } 
// Set the fields to specify which types of place data to 
// return after the user has made a selection. 
List<Place.Field> fields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ADDRESS);
 // Start the autocomplete intent. 
Intent intent = new Autocomplete.IntentBuilder( AutocompleteActivityMode.FULLSCREEN, fields) .build(this);
 startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE); }

And get selected result in onresult method

Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                destinationAddress = place.getAddress();
                lblDestinationAddress.setText(destinationAddress);

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Toast.makeText(this, "Some went wrong. Search again", Toast.LENGTH_SHORT).show();
                Log.i(TAG, status.getStatusMessage());
            } 

Dependency

//search places implementation
 'com.google.android.libraries.places:places:2.0.0'
Related