Junior
What is the difference between Snackbar and Toast?
sobes.tech AI
Answer from AI
Snackbar is more flexible: it allows adding an action button, is attached to a specific View for proper positioning, and hides when interacting with scrollable content (e.g., when scrolling a list). Toast is a simpler, non-interactive popup message that appears above all elements and is not attached to a specific View.
Key differences:
| Feature | Snackbar | Toast |
|---|---|---|
| Action button | Yes | No |
| Positioning | Attached to a View | Displays over everything, not attached |
| Hiding | Hides when scrolling (in CoordinatorLayout) | Does not hide when interacting with content |
| Interactivity | User can interact with the button | User cannot interact |
| Class | com.google.android.material.snackbar.Snackbar |
android.widget.Toast |
Example of using Snackbar:
// To display a Snackbar in CoordinatorLayout
Snackbar.make(findViewById(R.id.coordinator_layout),
"File deleted",
Snackbar.LENGTH_LONG)
.setAction("Undo") {
// Action when clicking "Undo" button
}
.show()
Example of using Toast:
// For a simple notification
Toast.makeText(applicationContext,
"Operation completed",
Toast.LENGTH_SHORT)
.show()