🎨 UI – Views & Layouts · Lesson 17
Toast, Snackbar & Dialogs
Which one to use?
| Component | Blocks the user? | Use for |
|---|---|---|
| Toast | No | Tiny confirmation: "Saved" |
| Snackbar | No | Message + one action: "Deleted · UNDO" |
| AlertDialog | Yes | Important decision: "Delete permanently?" |
| Bottom sheet | Partly | Several options: sort, share, filters |
Toast
Kotlin
Toast.makeText(this, "Note saved", Toast.LENGTH_SHORT).show() // in an Activity Toast.makeText(requireContext(), R.string.saved, Toast.LENGTH_LONG).show() // in a Fragment
Snackbar with action
Kotlin
// From Student Diary – swipe to delete with UNDO
viewModel.delete(note)
Snackbar.make(binding.root, "\"${note.title}\" deleted", Snackbar.LENGTH_LONG)
.setAction("Undo") { viewModel.restore(note) }
.show()
// show it above a FAB / bottom navigation
Snackbar.make(binding.root, "No internet", Snackbar.LENGTH_INDEFINITE)
.setAnchorView(binding.fab)
.setAction("Retry") { loadData() }
.show()Confirm dialog
Kotlin
MaterialAlertDialogBuilder(this)
.setTitle("Delete note?")
.setMessage("\"Maths homework\" will be deleted permanently.")
.setIcon(R.drawable.ic_delete)
.setNegativeButton("Cancel", null) // just closes
.setPositiveButton("Delete") { dialog, _ ->
deleteNote()
dialog.dismiss()
}
.show()List and single-choice dialogs
Kotlin
// simple list
val actions = arrayOf("Edit", "Share", "Delete")
MaterialAlertDialogBuilder(this)
.setTitle("Note options")
.setItems(actions) { _, which ->
Toast.makeText(this, actions[which], Toast.LENGTH_SHORT).show()
}
.show()
// single choice (radio buttons)
val themes = arrayOf("Light", "Dark", "System default")
var selected = 2
MaterialAlertDialogBuilder(this)
.setTitle("Choose theme")
.setSingleChoiceItems(themes, selected) { _, which -> selected = which }
.setPositiveButton("Apply") { _, _ -> applyTheme(selected) }
.setNegativeButton("Cancel", null)
.show()
// multi choice
val subjects = arrayOf("Maths", "Physics", "Chemistry")
val checked = booleanArrayOf(true, false, false)
MaterialAlertDialogBuilder(this)
.setTitle("Subjects")
.setMultiChoiceItems(subjects, checked) { _, which, isChecked -> checked[which] = isChecked }
.setPositiveButton("OK", null)
.show()Dialog with a custom layout
XML
<!-- res/layout/dialog_rename.xml -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="24dp"
android:paddingTop="8dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilName"
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>Kotlin
val dialogBinding = DialogRenameBinding.inflate(layoutInflater)
MaterialAlertDialogBuilder(this)
.setTitle("Add subject")
.setView(dialogBinding.root)
.setNegativeButton("Cancel", null)
.setPositiveButton("Add") { _, _ ->
val name = dialogBinding.etName.text?.toString()?.trim().orEmpty()
if (name.isNotEmpty()) addSubject(name)
}
.show()Bottom sheet
Kotlin
// res/layout/sheet_sort.xml has three TextViews: tvNewest, tvPriority, tvTitle
val sheet = BottomSheetDialog(this)
val sheetBinding = SheetSortBinding.inflate(layoutInflater)
sheet.setContentView(sheetBinding.root)
sheetBinding.tvNewest.setOnClickListener { sortBy("newest"); sheet.dismiss() }
sheetBinding.tvPriority.setOnClickListener { sortBy("priority"); sheet.dismiss() }
sheetBinding.tvTitle.setOnClickListener { sortBy("title"); sheet.dismiss() }
sheet.show()Common mistakes
- Forgetting
.show()on Toast/Snackbar/dialog. - Using
applicationContextfor dialogs → crash "Unable to add window". Use the Activity (this) orrequireContext(). - Dialogs for every small message → annoying; prefer Snackbar.
Summary
- Toast = tiny message; Snackbar = message + action (Undo).
- MaterialAlertDialogBuilder for confirm, list, single and multi choice, or a custom view.
- BottomSheetDialog for option menus at the bottom.
✅ Practice
In a list screen, long-press an item to show a list dialog (Edit/Share/Delete). Delete asks for confirmation, then shows a Snackbar with Undo.
Frequently Asked Questions
Why is my Toast not showing?
You probably forgot .show() at the end, or you are calling it from a background thread. Toasts must be shown on the main thread.
Snackbar or Toast?
Prefer Snackbar inside your app: it follows Material design, sits above the bottom bar and can have an action like Undo. Use Toast for very short messages when no view is available.
Why does my dialog disappear on rotation?
A dialog created with the builder is tied to the Activity instance. For dialogs that must survive rotation, use a DialogFragment.
