Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🎨 UI – Views & Layouts  ·  Lesson 17

Toast, Snackbar & Dialogs

Which one to use?

ComponentBlocks the user?Use for
ToastNoTiny confirmation: "Saved"
SnackbarNoMessage + one action: "Deleted · UNDO"
AlertDialogYesImportant decision: "Delete permanently?"
Bottom sheetPartlySeveral options: sort, share, filters
9:41▮▮ 85%Notes⋮Physics notesChapter 4"Maths homework" deleted UNDO9:41▮▮ 85%Notes⋮Maths homeworkDue tomorrowDelete note?This cannot be undone.CANCELDELETE
Snackbar with action and a confirm dialog

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()
Tap DeleteDialogDelete note?Cancelnothing happensDeletedeleteNote()
Always confirm destructive actions

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()
9:41▮▮ 85%Notes⋮Maths homeworkHighSort byNNewest firstPPriorityTTitle A–Z
BottomSheetDialog with three options

Common mistakes

  • Forgetting .show() on Toast/Snackbar/dialog.
  • Using applicationContext for dialogs → crash "Unable to add window". Use the Activity (this) or requireContext().
  • 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.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicToolbar, Options Menu & Popup Menu Your progress: 17 / 43 (40%) · Lesson 18 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free