I know I can call functions from inside MaterialAlertDialogBuilder that uses the user input in the Dialog, but I want to know if there is a way (preferably a good practice) to wait for the user to input the value and the app to execute the proceeding code. I tried it using async .await() with kotlin-coroutines but did not figure it out.
Here's an example of the MaterialAlertDialogBuilder implementation.
val nameInputDialog = MaterialAlertDialogBuilder(requireContext())
val customAlertDialogView = LayoutInflater.from(requireContext())
.inflate(R.layout.dialod_input_et, null, false)
val inputNameDialog: EditText = customAlertDialogView.findViewById(R.id.input_dialog_et)
nameInputDialog.setView(customAlertDialogView)
.setTitle("Type the name")
.setPositiveButton("Accept") { dialog, _ ->
val nameToInput = inputNameDialog.text.toString()
if (nameToInput == "") {
Toast.makeText(requireContext(), "Type a name", Toast.LENGTH_SHORT)
.show()
makeDialogBoxAndSetGroupID()
} else if (nameToInput != "") {
GlobalScope.launch() {
nameToDisplay = async {
nameToInput
}
}
dialog.dismiss()
}
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}.show()