Skip to content

Instantly share code, notes, and snippets.

@johnjeremih
Last active February 16, 2022 18:43
Show Gist options
  • Save johnjeremih/191a984fde183791ed93c8045b7f2625 to your computer and use it in GitHub Desktop.
Save johnjeremih/191a984fde183791ed93c8045b7f2625 to your computer and use it in GitHub Desktop.
This is how the Custom result contract AddressAutoComplete.kt is called.
class AddressFragment : Fragment() {
private lateinit var binding: AddressFragmentBinding
private var launcher = registerForActivityResult(AddressAutoComplete()) { place ->
if (place != null) {
val geocoder = Geocoder(activity)
var address : Address? = null
try {
if (place.address != null) {
//Get the first result
address = geocoder.getFromLocationName(place.address, 1)[0]
}
} catch (e: IOException) {
e.printStackTrace()
}
Log.e("Fragment","Address $address")
with(binding){
if (address != null) {
streetAddressValue.text = "${address.subThoroughfare} ${address.thoroughfare}"
cityAddressValue.text = address.locality
stateAddressValue.text = address.adminArea
zipCodeAddressValue.text = address.postalCode
countryAddressValue.text = address.countryName
}
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = AddressFragmentBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setNavigationOnClickListener { launcher.launch() }
}
}
implementation 'com.google.android.libraries.places:places:2.5.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment