Skip to content

Instantly share code, notes, and snippets.

@john-lorrenz
Last active November 19, 2019 16:50
Show Gist options
  • Save john-lorrenz/72a07bfa962d12e95980946aa3ed9fc0 to your computer and use it in GitHub Desktop.
Save john-lorrenz/72a07bfa962d12e95980946aa3ed9fc0 to your computer and use it in GitHub Desktop.
Create dialogs
fun createBasicAlertDialog(){
val builder = AlertDialog.Builder(this)
builder.setTitle("[DIALOG TITLE]")
builder.setMessage("[DIALOG MESSAGE]")
builder.setPositiveButton("[YES TEXT]") { dialog, which ->
// Action on click yes
}
builder.setCancelable(false)
builder.show()
}
fun createAlertDialog(){
// instantiate dialog
val builder = AlertDialog.Builder(this)
// instantiate the view for the dialog
val viewInflated = layoutInflater.inflate(R.layout.dialog_signin, null)
// inflate the view in the dialog
builder.setView(viewInflated)
// set the dialog title
builder.setTitle("Title")
// Set up the buttons
builder.setPositiveButton("Go",
DialogInterface.OnClickListener { dialog, which ->
// do function here
// get the input text from the view and do as you need with it
val username = viewInflated.findViewById(R.id.username) as EditText
val password = viewInflated.findViewById(R.id.password) as EditText
println(username)
println(password)
// dismiss dialog after
dialog.dismiss()
})
builder.setNegativeButton("Cancel",
DialogInterface.OnClickListener { dialog, which ->
dialog.cancel()
})
builder.setCancelable(true) // can be set to false, to make the dialog undismissable
builder.show()
}
fun createBasicDialog(){
val dialog = Dialog(this)
dialog.setContentView(R.layout.dialog_signin)
// USEFUL DIALOG PROPERTIES
// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
// dialog.setCancelable(false)
// dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
dialog.show()
}
fun createMultiOptionDialog(){
val options = arrayOf("[FIRST OPTION TITLE]", "[SECOND OPTION TITLE]")
val builder = AlertDialog.Builder(this)
builder.setTitle("[DIALOG TITLE]")
builder.setItems(options){ _, which ->
if(which == 0){
//first option clicked, do this...
}else if(which == 1){
//second option clicked, do this...
}
}
builder.show()
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="password"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment