0

My search bar of PlaceAutoComplete loses focus when being clicked. Sometimes it's possible to click and search but it doesn't find any place and then loses focus again.

I already tried to follow the solution presented here: Android Google Places API, getAutocompletePredictions returns status 'PLACES_API_ACCESS_NOT_CONFIGURED'

I have an activity with both Google maps fragment and Google places fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/place_autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

I enabled the Google Places in my google developer console:

enter image description here

And of course I'm using the API key, and it's done correctly since the Google Maps functionality is working:

  <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

Could it be a dependency issue? My relevant dependencies:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places-compat:2.2.0'

I added the places-compat (last dependency) just because someone adviced for it, but it didn't help.

What could cause this problem? Thanks

sir-haver
  • 3,096
  • 7
  • 41
  • 85

1 Answers1

1
// In Your Activity / Fragment While Clicking SearchView Call This (Kotlin)

private var mAutoCompleteRequestCode: Int = 1

    if (!Places.isInitialized()) {
                    Places.initialize(this.context!!, getString(R.string.google_maps_key), Locale.US)
                } else {
                    val mFields = listOf(
                        Place.Field.ID,
                        Place.Field.NAME,
                        Place.Field.LAT_LNG,
                        Place.Field.ADDRESS,
                        Place.Field.ADDRESS_COMPONENTS
                    )
                    val intent =
                        Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, mFields).build(
                            this.context!!
                        )
                    startActivityForResult(intent, mAutoCompleteRequestCode)
                }



override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(
            mTAG,
            "" + requestCode + "\t" + resultCode + "\t" + data
        )

        if (requestCode == mAutoCompleteRequestCode) {
            if (resultCode == RESULT_OK) {
                val place = Autocomplete.getPlaceFromIntent(data!!)

                val mLatitude = place.latLng?.latitude.toString()
                val mLongitude = place.latLng?.longitude.toString()
                val mAddress = place.name

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                val status = Autocomplete.getStatusFromIntent(data!!)
                Constant.logE(mTAG, "Auto Complete Places Search Error : ", status.statusMessage!!)
            }
        } else {

        }
    }