Last active
February 16, 2022 18:43
-
-
Save johnjeremih/191a984fde183791ed93c8045b7f2625 to your computer and use it in GitHub Desktop.
This is how the Custom result contract AddressAutoComplete.kt is called.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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