State Management with ViewModel
# Chapter 17: Dialogs, Snackbars, and Toasts
1. Introduction
When building mobile applications, communicating with the user without completely leaving the current screen is essential. Whether it's a brief success message, an undo action, or a critical confirmation prompt, Android provides three primary mechanisms for temporary UI feedback: Toasts, Snackbars, and Dialogs.2. Learning Objectives
By the end of this chapter, you will be able to:-
Display short, non-interactive messages using
Toast.
-
Implement interactive, temporary notifications using
Snackbar.
-
Create and customize modal alert boxes using
AlertDialog.
- Understand when to use which feedback mechanism based on UX best practices.
3. Core Concepts & Implementation
Toasts
AToast is a simple popup message that appears near the bottom of the screen. It is non-blocking (users can continue interacting with the app) and disappears automatically.
Use Case: Simple feedback, like "Draft saved."
*Note: The duration can be Toast.LENGTH_SHORT (approx 2 seconds) or Toast.LENGTH_LONG (approx 3.5 seconds).*
Snackbars
Introduced with Material Design,Snackbar is a modern alternative to Toast. It appears at the bottom of the screen, can contain a single action (like "UNDO"), and can be swiped away by the user.
Use Case: Feedback that might require a quick user action, like "Item deleted [UNDO]".
To use Snackbar, you need the Material Components library (included by default in modern Android projects).
AlertDialogs
AnAlertDialog is a modal window that overlays the screen. It demands the user's attention and requires them to make a choice or acknowledge a message before continuing.
Use Case: Destructive actions (e.g., "Are you sure you want to delete your account?") or requiring explicit user choice.
Custom Dialogs
You can also inflate your own XML layout into an AlertDialog to create fully custom popups (e.g., a mini login form).4. Best Practices
- Toasts: Use sparingly. They cannot be interacted with and might overlap with the keyboard.
-
Snackbars: The preferred method for brief, non-critical feedback in modern Android apps. Always attach them to the correct root view (e.g., a
CoordinatorLayoutallows the Snackbar to slide up and push floating action buttons out of the way).
- Dialogs: Use only for critical interruptions. Do not use dialogs for simple information; interrupting the user's workflow is a poor UX pattern if overused.
5. Exercises
- 1. Create a screen with a "Save" button. When clicked, show a Toast saying "Saving...".
- 2. Create a "Delete File" button. When clicked, show an AlertDialog asking for confirmation. If the user clicks "Yes", show a Snackbar saying "File Deleted" with an "Undo" button.
6. Coding Challenge
The Note Taker App Flow Create a mock screen for a Note app.-
1.
When the user clicks the "Clear All Notes" button, show a critical
AlertDialogwarning them.
-
2.
If they confirm, update a
TextViewto say "0 Notes".
-
3.
Immediately show a
Snackbarthat says "Notes cleared" with an "UNDO" action.
-
4.
If they click "UNDO" on the Snackbar, update the
TextViewback to "5 Notes" and show aToastsaying "Action undone".
7. Multiple Choice Questions (MCQs)
- 1. Which component is best suited for showing a message that requires a quick user action like "Undo"?
- a) Toast
- b) Snackbar
- c) AlertDialog
- d) Notification
-
2.
How do you prevent an
AlertDialogfrom closing when the user taps outside of it?
-
a)
dialog.setCanceledOnTouchOutside(false)
-
b)
dialog.setCancelable(false)
-
c)
builder.setCancelable(false)
- d) Both b and c
-
3.
What is the first argument required by
Toast.makeText()?
- a) A View
- b) A String
-
c) A Context (e.g.,
thisin an Activity)
- d) An Intent
-
4.
True or False: You can add an image to a default
AlertDialogusing the Builder pattern.
-
a) True, using
builder.setIcon()
- b) False, you must use a Custom View.
8. Interview Questions
- 1. What is the difference between a Toast and a Snackbar?
- 2. When should you use an AlertDialog instead of a Snackbar?
- 3. How do you avoid memory leaks when showing Dialogs?
onDestroy() or onStop() method of your Activity/Fragment if they are currently showing, to prevent "Activity has leaked window" exceptions.