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! :)
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With