Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Place Autocomplete Fragment: Unable to set text

Google has recently updated their Places SDK for android, so now I'm updating my code too. I'm trying to use the AutocompleteSupportFragment to allow the user to set their address.

This is my code:

mAddressEditText = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.address);
mAddressEditText.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG));
mAddressEditText.setHint("Address");
mAddressEditText.setText("Test1");                      // Works fine at the beginning, disappears after selecting a place and shows only the hint
mAddressEditText.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.d(TAG, "Place Selected");
        // Other Stuff
        mAddressEditText.setText("Test2");              // Doesn't Work, all I can see is the hint
        mAddressEditText.setText(place.getAddress());   // Doesn't Work, all I can see is the hint
    }

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

In the previous SDK, the fragment would set the text to the selected address automatically. This doesn't work in the new SDK (not sure if that's intentional or not). So I'm trying to set it manually instead. As you can see in the comments in my code, using setText works fine outside the listeners. Inside the listener they don't.

Am I doing something wrong or is this a bug?

EDIT: So long and I still can't get a proper fix to this. To be perfectly clear, I can get the address correctly from the fragment, the only thing that doesn't work is setText.

However, since some answers state they're not getting the same problem, I started thinking it might be related to the library versions I'm using?

These are the libraries I have in my build.gradle:

api 'com.android.support:appcompat-v7:28.0.0'
api 'com.android.support:support-annotations:28.0.0'

api 'com.android.support:multidex:1.0.3'

api 'com.google.firebase:firebase-core:16.0.8'
api 'com.google.firebase:firebase-auth:16.2.1'
api 'com.google.firebase:firebase-firestore:18.2.0'
api 'com.google.firebase:firebase-storage:16.1.0'
api 'com.google.android.libraries.places:places:1.1.0'
like image 445
mewais Avatar asked Feb 03 '19 02:02

mewais


1 Answers

setText has been giving me the same problem - it must be a bug I think. However I found a little work around with the hint. In your onPlaceSelected you can put the following:

Java

EditText etPlace = (EditText) autocompleteFragment.getView().findViewById(R.id.places_autocomplete_search_input);
etPlace.setHint(place.getAddress())

Kotlin

val etPlace = autocompleteFragment.view?.findViewById(R.id.places_autocomplete_search_input) as EditText
etPlace.hint = place.address
like image 96
CacheMeOutside Avatar answered Sep 29 '22 07:09

CacheMeOutside