Lesson 5 - Supplement 3 - Dialogs, Toast and Snackbar
Lesson 5 - Supplement 3 - Dialogs, Toast and Snackbar
Snackbar Widgets
Android Dialogs
Message
3
Build an alert dialog
The AlertDialog class lets you build a variety of dialog designs and is often the only
dialog class you need. As shown in the following figure, there are three regions of
an alert dialog:
• Title
• Content area
• Action buttons
The AlertDialog.Builder class provides APIs that let you create an AlertDialog with
these kinds of content, including a custom layout.
4
Add buttons
5
Add buttons - Example
6
Adding a list
AlertDialog.Builder(this)
.setTitle("Select an option")
.setItems(R.array.items) { dialog, which ->
println("Item selected - $which")
}
.create().show()
7
Adding a list
AlertDialog.Builder(this)
.setTitle("Select an option")
.setMultiChoiceItems(items, checked) { dialog, which, isChecked ->
println("Item selected - $which - $isChecked")
}
.setPositiveButton("OK") { dialog, which ->
println("OK")
}
.setNegativeButton("Cancel", null)
.create().show()
8
Adding a list
AlertDialog.Builder(this)
.setTitle("Select an option")
.setSingleChoiceItems(items, 0) { dialog, which ->
println("Item selected - $which")
}
.setPositiveButton("OK") { dialog, which ->
println("OK")
}
.setNegativeButton("Cancel", null)
.create().show()
9
Create a custom layout
When the user taps an action button created with an AlertDialog.Builder, the
system dismisses the dialog for you.
The system also dismisses the dialog when the user taps an item in a dialog list,
except when the list uses radio buttons or checkboxes. Otherwise, you can
manually dismiss your dialog by calling dismiss().
10
Custom layout example
XML Layout – custom_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Enter your information:"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edit_fullname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Enter your full name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
11
Custom layout example
XML Layout – custom_dialog_layout.xml
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_email" />
<Button
android:id="@+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="OK"
app:layout_constraintEnd_toStartOf="@+id/button_cancel"
app:layout_constraintTop_toBottomOf="@+id/edit_email" />
<EditText
android:id="@+id/edit_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Enter your email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_fullname" />
</androidx.constraintlayout.widget.ConstraintLayout>
12
Custom layout example
DialogActivity.kt
fun showCustomDialog() {
val dialog = Dialog(this)
dialog.setContentView(R.layout.dialog_custom_view)
val editFullName = dialog.findViewById<EditText>(R.id.edit_fullname)
val editEmail = dialog.findViewById<EditText>(R.id.edit_email)
dialog.findViewById<Button>(R.id.button_ok).setOnClickListener {
val fullName = editFullName.text.toString()
val email = editEmail.text.toString()
dialog.dismiss()
}
dialog.findViewById<Button>(R.id.button_cancel).setOnClickListener {
dialog.dismiss()
}
dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
dialog.show()
}
}
13
Create a dialog fragment
14
Create a dialog fragment – Example
StartGameDialogFragment().show(supportFragmentManager, "GAME_DIALOG")
}
}
15
DatePickerDialog
// Show dialog
val datePickerDialog = DatePickerDialog(this, {
view: DatePicker, year: Int, month: Int, day: Int ->
println("$year-${month + 1}-$day")
}, mYear, mMonth, mDay)
datePickerDialog.show()
16
TimePickerDialog
// Show dialog
val timePickerDialog = TimePickerDialog(this, {
view: TimePicker, hourOfDay: Int, minute: Int ->
println("$hourOfDay:$minute")
}, mHour, mMinute, false)
timePickerDialog.show()
17
Toast widget
A toast provides simple feedback about an operation in a small popup. It only fills
the amount of space required for the message and the current activity remains
visible and interactive. Toasts automatically disappear after a timeout.
For example, clicking Send on an email triggers a "Sending message..." toast, as
shown in the following screen capture:
18
Toast widget
19
Toast widget
20
Snackbar widget
21
Using snackbar
22
Showing a snackbar
Calling make creates the snackbar, but doesn't cause it to be visible on the
screen. To show it, use the show method on the returned Snackbar
instance. Note that only one snackbar will be shown at a time. Showing a
new snackbar will dismiss any previous ones first.
To show a snackbar with a message and no action:
23
Adding an action
24