Ultimate Guide to Material 3 Snackbars in Android XML (2025 Tutorial + Examples)

Ultimate Guide to Material 3 Snackbars in Android: Tutorial & Examples for 2025 🚀

Overview 📖

Material 3 Snackbars provide brief, temporary messages about app processes at the bottom of the screen, with optional actions. This guide covers their implementation, accessibility, and theming for 2025. 📢

Overview of Material 3 Snackbar

Design and API Documentation 🔗

Using Snackbars 🤖

Add the Material Components for Android library dependency to use Material 3 Snackbars. See the Get Started guide. Use Snackbar.make to create and show to display, ideally within a CoordinatorLayout for features like swipe-to-dismiss. Use a Theme.Material3.* theme for proper styling. 🛠️

Making Snackbars Accessible ♿

Snackbars are readable by TalkBack, with text labels auto-provided to accessibility services. Additional android:contentDescription is usually unnecessary but can be added for clarity. Ensure sufficient contrast for text visibility. See the Android accessibility guide. 🗣️

Behavior and Configuration 🎬

Configure snackbars with text, duration (LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE), and optional actions. Anchor to specific views like a FAB using setAnchorView. Example:

      
val contextView = findViewById(R.id.context_view)
Snackbar.make(contextView, "Action completed", Snackbar.LENGTH_SHORT)
    .setAction("Undo") { /* Handle undo */ }
    .show()
      
      
    

Snackbar Anatomy 🌟

Material 3 Snackbars consist of a container, text label, and optional action button, appearing temporarily at the screen’s bottom. 📢

Snackbar anatomy diagram
  • Container: Holds content, 6dp elevation.
  • Text Label: Brief message. ✍️
  • Action: Optional interactive button. 🖱️

Snackbar Example 🔲

Snackbars deliver non-intrusive feedback with optional actions, ideal for undo or retry scenarios. 📢

Snackbar example

Snackbar Example 💻

API and source code:

The following example shows a snackbar with an action button.

      

    

      
      
    
      
val contextView = findViewById(R.id.context_view)
Snackbar.make(contextView, "Item deleted", Snackbar.LENGTH_LONG)
    .setAction("Undo") { /* Handle undo */ }
    .show()
      
      
    

Anatomy and Key Properties 📏

A snackbar includes a container, text label, and optional action button. 📢

Snackbar anatomy diagram
  • Container: 6dp elevation, 8dp margin.
  • Text Label: Message, medium typography. ✍️
  • Action: Optional, dismisses on click. 🖱️

Attributes 📋

Element Attribute Related method(s) Default value
Container app:backgroundTint setBackgroundTintList ?attr/colorSurfaceInverse
Text android:text setText null
Action android:textColor setActionTextColor ?attr/colorPrimaryInverse
Animation app:animationMode setAnimationMode fade

Styles 🎨

  • Default style: Widget.Material3.Snackbar
  • Button style: Widget.Material3.Button.TextButton.Snackbar
  • Text style: Widget.Material3.Snackbar.TextView
  • Default theme attributes: ?attr/snackbarStyle, ?attr/snackbarButtonStyle, ?attr/snackbarTextViewStyle

See the full list of styles and attrs. 🔗

Theming Snackbars 🖌️

Material Theming customizes snackbar colors and typography. 🎨

Themed Snackbar

Theming Example 💻

Theming example for snackbars:

      

      
      
    

Specific styles for snackbars without affecting other components:

      



      
      
    

FAQ ❓

What are Material 3 Snackbars? 🤔

Brief, temporary messages at the screen’s bottom. ✅


When to use Snackbars? 🔢

Use for feedback like undo or retry, unlike Toasts for system notifications. 📢


How to add an action? 🖋️

Use setAction to add a clickable button. 🖱️


How to make them accessible? ♿

Text is auto-readable by TalkBack; add contentDescription if needed. 🗣️


Can I customize appearance? 🎨

Yes, theme colors and typography via res/values/styles.xml. 🖌️


How to add the Material 3 library? 📦

Include the Material Components for Android library. See the Get Started guide. 🛠️


Are there updates for 2025? 🗓️

This guide reflects 2025 Material 3 standards for snackbars. 🚀

Comments

Popular posts from this blog

Creating Beautiful Card UI in Flutter

Master Web Development with Web School Offline

Jetpack Compose - Card View