Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created April 20, 2017 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/45f608790113af7eb8e59ace78e77b1e to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/45f608790113af7eb8e59ace78e77b1e to your computer and use it in GitHub Desktop.
//Part of Functions.kt, this and below 3 functions helps to create dialog.
public fun Context.showDialog(cancelable: Boolean = false, cancelableTouchOutside: Boolean = false, builderFunction: AlertDialog.Builder.() -> Any) {
val builder = AlertDialog.Builder(this)
builder.builderFunction()
val dialog = builder.create();
dialog.setCancelable(cancelable)
dialog.setCanceledOnTouchOutside(cancelableTouchOutside)
dialog.show()
}
public fun AlertDialog.Builder.positiveButton(text: String = "OK", handleClick: (i: Int) -> Unit = {}) {
this.setPositiveButton(text, { dialogInterface, i -> handleClick(i) })
}
public fun AlertDialog.Builder.negativeButton(text: String = "CANCEL", handleClick: (i: Int) -> Unit = {}) {
this.setNegativeButton(text, { dialogInterface, i -> handleClick(i) })
}
public fun AlertDialog.Builder.neutralButton(text: String, handleClick: (i: Int) -> Unit = {}) {
this.setNeutralButton(text, { dialogInterface, i -> handleClick(i) })
}
//Sample of this code in MainActivityViewModel
activity.showDialog {
setTitle("Pick Path")
positiveButton("Pick Files", { pickPathToRead() })
negativeButton()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment