Skip to content

Instantly share code, notes, and snippets.

@Akhu
Created January 21, 2020 09:57
Show Gist options
  • Select an option

  • Save Akhu/d25bc1cf96cebaa52011aaa4e7179b44 to your computer and use it in GitHub Desktop.

Select an option

Save Akhu/d25bc1cf96cebaa52011aaa4e7179b44 to your computer and use it in GitHub Desktop.
Dialog Fragment with closure to get the content of EditText
class RegisterDialogFragment : DialogFragment() {
var onPositiveClick: ((userName: String) -> Unit)? = null
var onNegativeClick: (() -> Unit)? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val editText = EditText(context)
editText.inputType = InputType.TYPE_CLASS_TEXT
return with(AlertDialog.Builder(context)) {
setView(editText)
setTitle("Create a new Profile")
setMessage("Enter an astronaut Pseudo")
setPositiveButton("Register") { _, _ -> onPositiveClick?.invoke(editText.text.toString()) }
setNegativeButton("Cancel") { dialog, _ ->
dialog.cancel()
onNegativeClick?.invoke() }
create()
}
}
}
// Usage
fun showDialog() {
val dialog = RegisterDialogFragment()
dialog.onPositiveClick = {
val myEditTextValue = it
}
activity?.supportFragmentManager?.let { dialog.show(it, "RegisterDialog") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment