how to listen to AutocompleteSupportFragment no result found response

47
March 03, 2019, at 7:10 PM

I am trying to learn how to use the AutocompleteSupportFragment but I noticed the PlaceSelectionListener only has the onPlaceSelected and onError methods, it does not offer a method that is invoked when it does not find a place. How can I listen to no result found ?

Below is my code

if ( !Places.isInitialized() ) {
    Places.initialize( getApplicationContext(), getString( R.string.GOOGLE_MAPS_API_KEY ) );
    autoCompleteFragment = (AutocompleteSupportFragment) getSupportFragmentManager()
            .findFragmentById( R.id.autoCompleteFragment );
    autoCompleteFragment.setHint( getString( R.string.search_address_hint ) );
    autoCompleteFragment.setPlaceFields( Arrays.asList( Place.Field.ID,
            Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG ) );
    autoCompleteFragment.setCountry( "ZM" );

    btnConfirm.setOnClickListener( v -> confirmAddress() );
    autoCompleteFragment.setOnPlaceSelectedListener( new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            edtBuilding.setText( place.getName() );
            startIntentService( place.getLatLng() );
            adjustCamera( place.getLatLng() );
            mPresenter.addMarker( place.getLatLng(), place.getName(), place.getAddress(), ConfirmPickUpActivity.MARKER_NORMAL );
        }
        @Override
        public void onError(Status status) {
            Log.i( TAG, "An error occurred: " + status );
        }
    } );
}
Answer 1

You probably missed this page. https://developers.google.com/places/android-sdk/reference/com/google/android/libraries/places/api/net/PlacesStatusCodes

It is there, status code NOT_FOUND. In your code you have to write.

@Override
    public void onError(Status status) {
        Log.i( TAG, "An error occurred: " + status.getStatusCode () );
    }