6

Is it possible that when I click on the autocomplete, it will set the current address as the default string? Pretty much like this post but I can't find any decent answer. Please help. Thank you! :)

ayabbear
  • 308
  • 4
  • 20

1 Answers1

3

Despite it is already 3 years later, I decided to answer in case there is someone needs it. Since startActivityForResult is deprecated, I go for the new way to handle activity results here. When you are creating intent to open Autocomplete activity, you should apply setInitialQuery("Hawaii") before building it. Here is the whole code:

var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data

        data?.let {
            val place = Autocomplete.getPlaceFromIntent(data)
            makeToast(place.name.toString()) // <- Consume your result
        }
    }
}

private fun openSearchAutocomplete() {
    val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)

    val intent = Autocomplete.IntentBuilder(
        AutocompleteActivityMode.OVERLAY, fields).setCountry("US") //United States
        .setInitialQuery("Hawaii") // <- set initial query here
        .build(this)
    resultLauncher.launch(intent)
}
Nodirxon
  • 99
  • 9